Rolling Windows & Moving Averages
Raw time series are often noisy. Rolling windows smooth that noise and reveal trends by computing a statistic over a sliding window of recent observations. The classic example is a moving average, but the same mechanism powers rolling sums, rolling standard deviations, and expanding (cumulative) statistics.
What You'll Learn
- How to compute a moving average with
rolling - How window size affects smoothing
- How to use
min_periodsto handle the start of a series - The difference between rolling and expanding windows
The Moving Average
rolling(window=N) defines a sliding window of N rows. Chaining an aggregation like mean computes that statistic for each window position. This is the standard way to smooth a series.
The first two ma_3 values are NaN because a full 3-row window is not yet available. From the third row onward, each value is the average of the current row and the two before it.
Window Size Controls Smoothing
A larger window smooths more aggressively but reacts more slowly to change. Compare a short and long window on the same series.
The 5-day average is steadier, while the 3-day average follows the data more closely. Choosing the window is a tradeoff between smoothing and responsiveness.
Other Rolling Statistics
rolling is not limited to the mean. You can take a rolling sum, max, min, or standard deviation, which is useful for measuring recent volatility.
min_periods: Values From the Start
By default a window needs to be full before it produces a value. Set min_periods to allow partial windows at the beginning, so you get numbers from the very first row.
The ma_partial column has no leading NaN values because partial windows are allowed.
Expanding Windows
An expanding window grows to include every row seen so far, giving a cumulative statistic rather than a fixed-size one. expanding().mean() is the running average of the whole series up to each point.
Exercise: 3-Day Moving Average
Exercise: Running Total
Key Points
rolling(window=N).mean()computes a moving average over a sliding window- Larger windows smooth more but react slower to change
rollingalso supportssum,max,min, andstdfor recent volatilitymin_periodslets partial windows return values at the start of the seriesexpandinggrows the window cumulatively for running statistics
اختبار قصير
التالي · الدرس 37 من 42
Shifting, Lagging & Period-over-Period Change
قراءة 2 دقيقة
أنهيت 36 من 42 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

