-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Open
Labels
Description
Is your feature request related to a problem?
With a group.apply
the applied function acts on all columns available to provide a result. With rolling.apply
it applies the function to each column individually - it would be great if you could apply it to all the columns as well.
e.g. the following is possible with groupby.apply
df = pd.DataFrame({'A': range(10), 'B': range(10,0,-1)}).reset_index()
df['index'] = df['index'] % 3
df.groupby('index').apply(lambda df: df['A'] + df['B'])
However, you can't do the same equivalent with rolling.apply
:
df = pd.DataFrame({'A': range(10), 'B': range(10,0,-1)}).reset_index()
df.rolling(3, on='index').apply(lambda df: (df['A'] * df['B']).sum())
Gives the error: KeyError: 'A'
This is because the apply
function is applied to each column separately. The series is passed to the function not the dataframe. This feels inconsistent and annoying if you want to apply a function to the grouped rolling data. It would be great if there were a way to do it 😃