0

How can you pass a function into pd.groupby and call it from there? I have a series with time index and values. Let's say I want to group it by month and call the sum function. But it could be any other (size, average etc.). I want to pass this into the function.

import pandas as pd
                   #MONTH1 ... #M2
series = pd.Series([1, 2, 2, 2, 5]) # assume time-index on daily observations on Month 1 and Month 2


def custom_calc(series:pd.Series, freq, func):
      grouper = series.groupby(pd.Grouper(freq)).func()
      return grouper


Output >>
 custom_calc(series, "M", sum) 

# It combines month 1 observations into a single sum, and month 2 leaves it as it is only 1 observation.


pd.Series([1+2+2+2, 5])
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
matt.aurelio
  • 381
  • 2
  • 9
  • 1
    IIUC, you're asking how to access a method of a `SeriesGroupBy` object. There's an existing question about how to do that in general, so I've closed your question as a duplicate. You want something like `grouper = series.groupby(...); result = getattr(grouper, func)(); return result` followed by `custom_calc(..., 'sum')`. – wjandrea Jun 28 '22 at 15:47
  • I honestly don't see right now how those questions are related but OK. – matt.aurelio Jun 28 '22 at 16:53
  • You wrote `.func`, but the attribute name `func` is looked up on the `SeriesGroupBy` object, not the local scope (where the `func` variable resides). To get what you want, you use `getattr(..., func)`. – wjandrea Jun 28 '22 at 17:01
  • 1
    Got it! thanks! Quick question, how to get a list of all possible attributes from SeriesGroupBy object? – matt.aurelio Jun 28 '22 at 17:04

0 Answers0