Skip to content

DOC: Rolling with method="table" to apply func to all columns and not just individual columns #40374

@joshlk

Description

@joshlk

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 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions