Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

.rolling_mean

Computes a fixed-size moving mean over an expression or Series. It supports optional elementwise weights, a minimum non-null threshold, and centered output labels.

How it works

rolling_mean evaluates a fixed-size moving window over an expression or Series and aggregates each window to its mean. The current row and preceding window_size - 1 rows are included; optional weights, a minimum non-null count, and centered labels change how results are produced.

Syntax

Expr.rolling_mean(window_size: int, weights: list[float] | None = None, *, min_samples: int | None = None, center: bool = False) → Expr

Parameters

NameTypeDescription
window_sizeintLength of the window in number of elements.
weightslist[float] | NoneOptional slice with the same length as the window, multiplied elementwise with window values; expression weights are normalized to sum to 1.
min_samplesint | NoneNumber of non-null window values required before computing a result; defaults to window_size when None.
centerboolWhether to set labels at the center of the window.

Returns

A Series for polars.Series.rolling_mean or an Expr for polars.Expr.rolling_mean.

Key points

  • Computes a moving average over the values in a Series or expression.
  • The window length is controlled by the integer window_size argument.
  • By default, each output uses the current row plus preceding rows.
  • Optional weights apply elementwise to values inside each window.
  • After window values and optional weights are handled, the result is their mean.
  • min_samples specifies the non-null count needed before producing a result.

Common mistakes

  • With the default min_samples=None, a result requires a complete window of non-null values.
  • center=True changes result labeling by centering each window instead of using the trailing position.
  • For expression rolling means, supplied weights are normalized before they are applied.

Example: Trailing rolling mean

import polars as pl

df = pl.DataFrame({"value": [100, 200, 300, 400, 500]})
result = df.with_columns(
    pl.col("value").rolling_mean(window_size=2).alias("mean_2")
)
print(result)

Adds a two-element trailing rolling mean to the DataFrame.

When to use it

Trend smoothing

Smooth noisy numeric columns while retaining one output per input row.

Feature engineering

Calculate short-window averages in a DataFrame transformation.

Weighted averages

Apply custom relative importance to values within each fixed window.

Interview questions

QWhat does rolling_mean calculate?

It computes a moving mean over a fixed-length window. At each row, the window contains that row and the preceding window_size - 1 elements.

Follow-up: How would you avoid early partial results? Set min_samples to window_size, which is also the default when it is None.

QWhat are the main parameters?

window_size is the number of elements in each window. weights optionally supplies one multiplier per window element, while min_samples controls how many non-null values are required.

Follow-up: What constraint applies to weights? The weights must have the same length as the window; expression weights are normalized to sum to one.

QHow is it used in a DataFrame expression?

The method returns a Series when called on a Series and an Expr when called on an expression. The expression form can be used inside DataFrame transformations such as with_columns.

Follow-up: How can a window be aligned differently? Pass center=True to set labels at the center of the window.

  • polars.Series.rolling_mean
  • polars.Expr.rolling_mean_by
  • polars.Expr.rolling_sum
  • polars.Expr.rolling_min
  • polars.Expr.rolling_max

Source: polars.Expr.rolling_mean — Polars documentation. Checked 2026-10-15.