pandas.api.typing.Rolling.apply — pandas 3.0.0rc0+33.g1fd184de2a documentation (original) (raw)

Rolling.apply(func, raw=False, engine=None, engine_kwargs=None, args=None, kwargs=None)[source]#

Calculate the rolling custom aggregation function.

Parameters:

funcfunction

Must produce a single value from an ndarray input if raw=Trueor a single value from a Series if raw=False. Can also accept a Numba JIT function with engine='numba' specified.

rawbool, default False

If you are just applying a NumPy reduction function this will achieve much better performance.

enginestr, default None

engine_kwargsdict, default None

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.

argstuple, default None

Positional arguments to be passed into func.

kwargsdict, default None

Keyword arguments to be passed into func.

Returns:

Series or DataFrame

Return type is the same as the original object with np.float64 dtype.

See also

Series.rolling

Calling rolling with Series data.

DataFrame.rolling

Calling rolling with DataFrames.

Series.apply

Aggregating apply for Series.

DataFrame.apply

Aggregating 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