Skip to main content
Albero Academy

Rolling & Window Functions

Moving averages, cumulative sums and period-over-period change.

Time Series & Advanced9 min

Window functions compute over a sliding range of rows — moving averages to smooth noise, cumulative totals, and comparisons to previous periods. They're essential for trends and time-series features.

Rolling windows

moving average
ts["ma7"] = ts["sales"].rolling(7).mean() # 7-day moving average
ts["max30"] = ts["sales"].rolling(30).max() # 30-day rolling max
ts["cumsum"] = ts["sales"].cumsum() # running total

shift, diff & pct_change

compare to the past
ts["prev"] = ts["sales"].shift(1) # yesterday's value
ts["change"] = ts["sales"].diff() # absolute change vs previous
ts["growth"] = ts["sales"].pct_change() # percent change

Key takeaways

  1. 01rolling(n) computes over a sliding window — moving average, rolling max, etc.
  2. 02cumsum/cumprod give running totals.
  3. 03shift moves values by n rows; diff and pct_change compare to the previous period.
  4. 04The first window-1 rolling values are NaN by design.

Finished this chapter?

Mark it complete to track your progress through Pandas.