Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add support for rolling apply on multiple columns or whole DataF… #58501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

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
1 participant