[Python] Pandas dt accessor or groupby function returning decimal numbers instead of integers...

Discussão em 'Python' iniciado por Stack, Julho 29, 2025.

  1. Stack

    Stack Membro Participativo

    We're trying to group up date counts by month and index values are returning as decimals instead of integers when series contain any number of NaTs / na values.

    Simplified reproducible example:

    import pandas as pd

    df = pd.DataFrame({"A": ["2025-07-24","2025-07-24","2025-07-24"], "B": ["2025-07-24","2025-07-24",pd.NA]}, dtype="datetime64[ns]")

    df['values'] = [1,2,3]

    a_df = df.groupby([df["A"].dt.month])["values"].count()
    b_df = df.groupby([df["B"].dt.month])["values"].count()

    print(a_df)
    print(b_df)


    So the index value for a_df is "7" and the index value for b_df is "7.0", with an undesired ".0" suffix.

    What's causing this and what's a good way to make values return as integers, or at least return consistently?

    Continue reading...

Compartilhe esta Página