Skip to content

Conversation

Jruth44
Copy link

@Jruth44 Jruth44 commented May 1, 2024

ENH: Add support for rolling apply on multiple columns or whole DataFrame

This pull request adds support for applying a function to multiple columns or the entire DataFrame when using the rolling.apply method. The new multi_column parameter has been introduced to enable this functionality.

Key changes:

  • Modified the _apply function in pandas/core/window/rolling.py to handle the multi_column parameter and apply the function accordingly.
  • Added test cases in pandas/tests/window/test_rolling_apply.py to cover the new functionality.
  • Added an entry in the release notes (doc/source/whatsnew/v3.0.0.rst) to document the enhancement.

Usage example:

import pandas as pd
import numpy as np

data = {'value': [np.nan, np.nan, np.nan, 10],
        'weight': [np.nan, np.nan, 2, 3]}
df = pd.DataFrame(data)

def multi_column_func(window_df):
    return window_df['value'] * window_df['weight']

result = df.rolling(window=2, min_periods=1).apply(multi_column_func, multi_column=True)
print(result)

0         NaN
1         NaN
2         NaN
3    30.0000
dtype: float64

@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

@mroeschke mroeschke closed this May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: rolling apply multiple columns or whole dataframe
2 participants