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

Pandas · Module

df.rolling

Creates sliding windows over a DataFrame for moving calculations. Configure the window, required observations, label placement, weighting, timing column, evaluation spacing, and execution mode before applying an aggregation or transformation.

How it works

Call DataFrame.rolling to create a rolling-window object, then apply an aggregation or transformation such as mean, sum, or std. The window may be a row count or a time-based interval; min_periods controls how many observations are needed before a result is emitted.

Syntax

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, closed=None, step=None, method='single')

Parameters

NameTypeDescription
windowint, timedelta, str, offset, or BaseIndexer subclassWindow size or interval; accepts an integer, timedelta, string, offset, or BaseIndexer subclass.
min_periodsint, default NoneMinimum observations required to produce a value; otherwise the result is NaN.
centerbool, default FalseWhether result labels are centered on their windows.
win_typestr, default NoneWindow weighting function name, or None for equal weights.
onstr, optionalDataFrame column label or index level used for the window instead of the DataFrame index.
stepint, default NoneNumber of result positions between evaluated windows.
method{'single', 'table'}, default 'single'Whether to process each column or row separately or the entire object.

Returns

A Rolling window object that supports rolling aggregations and transformations.

Key points

  • The method constructs rolling windows over a DataFrame so a subsequent operation can calculate moving statistics.
  • min_periods sets the minimum observations needed for a non-missing result; otherwise the position is missing.
  • center=True places result labels at the middle of each window rather than at its trailing edge.
  • With no win_type, each observation contributes equally to the windowed calculation.
  • method selects independent per-column or per-row processing versus whole-object table processing.

Common mistakes

  • When on is supplied, the named column or index level determines the time-based window instead of the DataFrame index.
  • Using step reduces the number of evaluated output positions, so the result is not calculated at every input row.

Example: Trailing rolling mean

import pandas as pd

sales = pd.DataFrame({"sales": [10, 20, 15, 30]})
sales["avg_2"] = sales["sales"].rolling(window=2, min_periods=1).mean()
print(sales)

Computes a two-row trailing mean while producing a value from the first row onward.

When to use it

Moving averages

Smooth noisy measurements or prices by replacing each point with a recent-window average.

Windowed statistics

Calculate rolling sums, minima, maxima, or standard deviations for operational monitoring.

Time-aware analysis

Use a time-based window to summarize observations over recent calendar intervals.

Interview questions

QWhat does DataFrame.rolling return?

It returns a rolling-window object rather than the final aggregated values. Apply an operation such as mean() afterward to calculate a statistic for each window.

Follow-up: How would you avoid early missing values? Set min_periods to a value no greater than the observations available in the initial windows.

QWhich kinds of windows can be specified?

window can be an integer, a time interval such as a string or offset, a timedelta, or a BaseIndexer subclass. An integer describes a number of observations, while time-based windows describe an interval.

Follow-up: When using a time-based window, what should determine the window? Use the index by default, or identify a column or index level with on.

QWhat do method and step control?

method='single' performs the rolling operation independently for each column or row. method='table' performs it across the whole object and is available as an alternative execution mode.

Follow-up: How can output density be changed? Pass step to evaluate only every specified number of result positions.

  • pandas.DataFrame.expanding
  • pandas.DataFrame.ewm
  • pandas.Series.rolling
  • pandas.api.indexers.BaseIndexer
  • pandas.core.window.Rolling

Source: pandas.DataFrame.rolling — pandas 3.0.6 documentation. Checked 2026-10-15.