[Python] How to pass argument to func in `pandas.resampler.agg()` when using dict input?

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to resample a pandas dataframe, and for some columns I would like to sum on. additionally, I want to get None/nan as result when there is no rows in a resampling period. For aggregation on a single column, I can do the following:

    df = pd.DataFrame(index=[pd.to_datetime('2020-01-01')], columns=['value'])
    df.resample('5min').agg("sum", min_count=1)


    according to pandas doc, the keyword argument min_count will be passed to resample.Resampler.sum associated with the string "sum". and the result is desired.

    value
    2020-01-01 None


    However, this won't work if I pass a dictionary as agg input, e.g.

    df = pd.DataFrame(index=[pd.to_datetime('2020-01-01')], columns=['value'])
    df.resample('5min').agg({'value': 'sum'}, min_count=1)


    will output:

    value
    2020-01-01 0


    I would like to know the right way to pass arguments to the aggregation functions specified inside the dict.

    Continue reading...

Compartilhe esta Página