Moving averages
Smooth noisy measurements or prices by replacing each point with a recent-window average.
Pandas · Module
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.
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.
DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, closed=None, step=None, method='single')| Name | Type | Description |
|---|---|---|
window | int, timedelta, str, offset, or BaseIndexer subclass | Window size or interval; accepts an integer, timedelta, string, offset, or BaseIndexer subclass. |
min_periods | int, default None | Minimum observations required to produce a value; otherwise the result is NaN. |
center | bool, default False | Whether result labels are centered on their windows. |
win_type | str, default None | Window weighting function name, or None for equal weights. |
on | str, optional | DataFrame column label or index level used for the window instead of the DataFrame index. |
step | int, default None | Number of result positions between evaluated windows. |
method | {'single', 'table'}, default 'single' | Whether to process each column or row separately or the entire object. |
A Rolling window object that supports rolling aggregations and transformations.
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.win_type, each observation contributes equally to the windowed calculation.method selects independent per-column or per-row processing versus whole-object table processing.on is supplied, the named column or index level determines the time-based window instead of the DataFrame index.step reduces the number of evaluated output positions, so the result is not calculated at every input row.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.
Smooth noisy measurements or prices by replacing each point with a recent-window average.
Calculate rolling sums, minima, maxima, or standard deviations for operational monitoring.
Use a time-based window to summarize observations over recent calendar intervals.
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.
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.
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.expandingpandas.DataFrame.ewmpandas.Series.rollingpandas.api.indexers.BaseIndexerpandas.core.window.Rolling