Knowledge base / Python
pandas rolling: row windows, time windows and missing data
I compare three records with three days and trace rolling means and missing-value handling.

When I want an average of recent observations, I use Series.rolling() or DataFrame.rolling() and then choose an aggregation. Calling rolling() defines the windows; mean(), sum() or another operation calculates the result. It is not a standalone pd.rolling() function.
This entry covers ordinary equally weighted windows using the pandas 3.1.1 documentation. All examples are synthetic. I use a small fixed dataset instead of random numbers so every average can be followed on paper. First, however, I establish whether “the last three” means three records or a period of three days.
An average of three consecutive rows
The Series.rolling documentation separates the window definition from the minimum observation count. For an integer window with the default closed="right", size 3 includes the current row and the two preceding rows. I also set min_periods=3 explicitly:
import pandas as pd
s = pd.Series([10, 20, 30, 40, 50], dtype="float64")
rolling_mean = s.rolling(window=3, min_periods=3).mean()
| Index | Value | Rolling mean |
|---|---|---|
| 0 | 10 | NaN |
| 1 | 20 | NaN |
| 2 | 30 | 20.0 |
| 3 | 40 | 30.0 |
| 4 | 50 | 40.0 |
The first complete window contains 10, 20 and 30, giving 60/3 = 20. The next contains 20, 30 and 40, giving 30. The final window contains 30, 40 and 50, giving 40. Initial NaN values reflect the three-observation requirement, rather than an import error. For these inputs, the documented Rolling.mean returns floating-point values.
min_periods and missing observations
With s.rolling(3, min_periods=1).mean(), the first two results become 10 and 15. The remaining values stay 20, 30 and 40. This permits shorter initial windows, so I would not label every point as an average of exactly three available values. It is easy to miss this distinction once the plotted line looks smooth.
Missing values inside a window matter too. For pd.Series([10.0, None, 30.0]), a size-3 window contains only two available numbers. Its final result is NaN with min_periods=3, but 20 with min_periods=2. Replacing the missing value with zero would produce different arithmetic. I first establish whether a gap means no measurement or genuinely no events; these meanings should not be exchanged to make a chart convenient.

Three days can contain a single record
A time window needs an appropriate datetime index or a DataFrame column specified with on. The following dates are ordered and deliberately contain a gap:
t = pd.Series(
[10.0, 20.0, 50.0],
index=pd.to_datetime(["2026-01-01", "2026-01-02", "2026-01-05"]),
)
by_rows = t.rolling(3, min_periods=3).sum()
by_time = t.rolling("3D", min_periods=1, closed="right").sum()
| Date | Three-row sum | 3D window sum |
|---|---|---|
| 2026-01-01 | NaN | 10.0 |
| 2026-01-02 | NaN | 30.0 |
| 2026-01-05 | 80.0 | 50.0 |
At January 5, the time window is (January 2, January 5]: it excludes the left boundary and includes the right one. Only 50 belongs to that window. The three-row window still includes all three records regardless of the gaps between dates. rolling does not insert missing days into the index. To change data frequency, see resample.
Default min_periods differs too. For an integer window it equals the window size, whereas for an offset window it defaults to 1. I specify it explicitly. This entry uses right-closed windows; left, both and neither change boundary inclusion. I would not reuse the table’s results under different settings without tracing which observations enter each window.
center and information available at calculation time
With center=False, results are labelled at the right edge of the window. Setting center=True centres the labels. For a regular series and size 3, a result at a middle point can then use the next observation. This can be useful for retrospective smoothing. For forecasting features, however, check whether every input was already available when the decision would have been made.
Even an ordinary right-aligned window includes the current value. If a feature must use earlier rows only, consider s.shift(1).rolling(3, min_periods=3).mean(). Series.shift without freq shifts values relative to the index. This is a shift by one record, so with irregular dates I would not describe it as “exactly one day earlier.” Feature design also needs the source’s actual availability time.
Other aggregations and a sensible scope
The same windows support sums, minima, maxima and medians. Choose the operation from the question: a sum serves a different purpose from an average. Standard deviation introduces a separate ddof choice and needs its own definition. The windowing operations guide covers aggregations and window variants more broadly.
win_type selects suitable SciPy window functions and may require additional parameters. I do not equate it with pandas exponentially weighted calculations, which use the separate Series.ewm interface. Nor would I promise that shrinking a window solves every performance problem. First define a correct result, then assess cost in the actual application.
Exercise: identify each window’s members
For the sequence 10, 20, 30, 40, 50, write the three values used at index 3. Under the default size-3 window, they are 20, 30 and 40, giving a mean of 30. Then remove the measurement 30 and consider the difference between deleting its row and retaining a row containing NaN. An observation-based window behaves differently in those two cases. Return to the data definition before smoothing the chart.
Key takeaways
Does rolling(3) mean three days?
With default boundaries it uses three consecutive rows. A time period such as rolling("3D") requires an appropriate datetime index.
Why are the initial results NaN?
The window may not contain enough available values to satisfy min_periods. For an integer window, its default is the window size.
Can I use center=True for forecasting features?
It can include observations later than the result label. Check that every input was available at decision time.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
