Pandas/Time series 9 min

Rolling windows and shifts

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

rollingshiftpct_change

rolling(n) creates a moving window over the previous n rows. It is how you smooth noisy daily series into a readable trend.

python · editable
loading editor…

Try it: Use min_periods=1 so the first rows are not NaN.

shift moves values along the index, so pct_change and diff give day-over-day change. expanding() accumulates from the start instead of a fixed window.

python · editable
loading editor…

Sort by time before any rolling or shift operation. On unsorted data the results are silently wrong rather than an error.