Calculate the rolling custom aggregation function.
Must produce a single value from an ndarray input if raw=True or a single value from a Series if raw=False. Can also accept a Numba JIT function with engine='numba' specified.
False : passes each row or column as a Series to the function.
True : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance.
'cython' : Runs rolling apply through C-extensions from cython.
'numba' : Runs rolling apply through JIT compiled code from numba. Only available when raw is set to True.
None : Defaults to 'cython' or globally setting compute.use_numba
For 'cython' engine, there are no accepted engine_kwargs
For 'numba' engine, the engine can accept nopython, nogil and parallel dictionary keys. The values must either be True or False. The default engine_kwargs for the 'numba' engine is {'nopython': True, 'nogil': False, 'parallel': False} and will be applied to both the func and the apply rolling aggregation.
Positional arguments to be passed into func.
Keyword arguments to be passed into func.
Return type is the same as the original object with np.float64 dtype.
See also
pandas.Series.rollingCalling rolling with Series data.
pandas.DataFrame.rollingCalling rolling with DataFrames.
pandas.Series.applyAggregating apply for Series.
pandas.DataFrame.applyAggregating apply for DataFrame.
Examples
>>> ser = pd.Series([1, 6, 5, 4])
>>> ser.rolling(2).apply(lambda s: s.sum() - s.min())
0 NaN
1 6.0
2 6.0
3 5.0
dtype: float64
© 2008–2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
© 2011–2025, Open source contributors
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/2.3.0/reference/api/pandas.core.window.rolling.Rolling.apply.html