polars.Series.rolling_apply#

Series.rolling_apply(function: Callable[[Series], Any], window_size: int, weights: list[float] | None = None, min_periods: int | None = None, center: bool = False) Series[source]#

Apply a custom rolling window function.

Prefer the specific rolling window functions over this one, as they are faster:

  • rolling_min

  • rolling_max

  • rolling_mean

  • rolling_sum

Parameters:
function

Aggregation function

window_size

The length of the window.

weights

An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

min_periods

The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

center

Set the labels at the center of the window

Examples

>>> import numpy as np
>>> s = pl.Series("A", [11.0, 2.0, 9.0, float("nan"), 8.0])
>>> print(s.rolling_apply(function=np.nanstd, window_size=3))
shape: (5,)
Series: 'A' [f64]
[
    null
    null
    3.858612
    3.5
    0.5
]