I suspect that this is very simple functionality in Python, and I looked at the suggestions in Questions that may already have an answer , but I don't think this is a duplicate question, I will delete it if there is one.
Task:
I would like to wrap df.groupby(pd.TimeGrouper(freq='M')).sum()in a function so that I can assign sum(), mean()or count()as arguments in that function. I asked a similar question earlier here , but I don’t think I can use the same technique in this particular case.
Here is a snippet with reproducible input:
import pandas as pd
import numpy as np
np.random.seed(12345678)
df = pd.DataFrame(np.random.randint(0,2,size=(100, 4)), columns=list('ABCD'))
datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=100).tolist()
df['dates'] = datelist
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
print(df.head(10))
What gives:

With this we can do:
df2 = df.groupby(pd.TimeGrouper(freq='M')).sum()
print(df2)
And we get:

Or we can do:
df3 = df.groupby(pd.TimeGrouper(freq='M')).mean()
print(df3)
And we get:

Here is the part of the procedure enclosed in the function:
def function1(df):
df = df.groupby(pd.TimeGrouper(freq='M')).sum()
return df
df4 = function1(df = df)
print(df4)
And it works great:

, sum() mean() Function2, :
def function2(df, fun):
df = df.groupby(pd.TimeGrouper(freq='M')).fun
return df
TypeError:
df5 = function2(df = df, fun = sum())

:
df6 = function2(df = df, fun = 'sum()')

, ? ( "" freq, ). , ?
!
:
import pandas as pd
import numpy as np
np.random.seed(12345678)
df = pd.DataFrame(np.random.randint(0,2,size=(100, 4)), columns=list('ABCD'))
datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=100).tolist()
df['dates'] = datelist
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
print(df.head(10))
df2 = df.groupby(pd.TimeGrouper(freq='M')).sum()
print(df2)
df3 = df.groupby(pd.TimeGrouper(freq='M')).mean()
print(df3)
def function1(df):
df = df.groupby(pd.TimeGrouper(freq='M')).sum()
return df
df4 = function1(df = df)
print(df4)
def function2(df, fun):
print(fun)
df = df.groupby(pd.TimeGrouper(freq='M')).fun
return df