pandas in depth · lesson 7 of 12
Parts in this lesson
As.resample("W").sum()days into weeks
Bs.rolling(7).mean()a 7-day average
Cs.shift(1), s.diff()yesterday, and the change since

Time series

Last updated

Sales by day, rainfall by hour, prices by month: data with a date on every value is a time series, and it has tools of its own. In this lesson you'll turn daily sales into weekly and monthly totals, smooth them with a rolling average, compare each day with the one before, fill in missing days, and see how a centred average quietly uses the future.

By the end of this lesson you can

  • Give a series a DatetimeIndex, and resample it to weeks and months
  • Smooth a series with rolling, and compare days with shift, diff and pct_change
  • Find and fill missing days with asfreq
  • Explain why center=True can’t be used for anything that predicts

Buckets and windows

The notebook makes 60 days of simulated rice sales for one store (with a weekly pattern and a seed, as in project 2.P), indexed by date. Step through what the main tools do:

Resampling and rolling windows. Daily rice sales with a date on each value: a time series. The numbers here are the notebook’s first eight days.43.241.842.342.450.957.448.247.8kilos, 1 to 8 January12345678
1 of 5 Daily rice sales with a date on each value: a time series. The numbers here are the notebook’s first eight days.
daily.ipynb
  • A Series whose index is dates (a DatetimeIndex) knows about time. pd.date_range made one here; with real data, set_index("date") after parse_dates.
  • resample("W").sum(): daily into weekly totals. "W" weeks end on Sunday, so each total is labelled with its Sunday. "MS" is calendar months, labelled with the first day. January sold 1,412.9 kg.
  • rolling(7).mean(): a 7-day average, which smooths away the weekly ups and downs so the trend shows. The first six days have no value (NaN): there aren't seven days yet.
  • shift(1) moves the series down one day, so each row can see yesterday. diff() is today minus yesterday.
  • pct_change(): the change as a share. February was 1.4% below January, but February had 29 days to January's 31. Per day, it sold more. Always ask whether the buckets are the same size.

Missing days

A store sends figures only for days it remembers. Run the cells.

gaps.ipynb

There's no row for 3 or 4 January, and nothing says so. A rolling average over "the last 3 rows" would quietly cover five days. asfreq("D") puts in a row for every day, with NaN where nothing was reported, so the gaps show. Then decide, as in lesson 3.8: fill_value=0 only if you know the store was closed. Otherwise leave them missing, and count them.

Wrong way: an average that sees the future

You're smoothing sales to plan next week's stock. Look again at the last frame of the figure in step 1, then run the cells.

centred.ipynb

The trailing average has a value for every day up to 29 February. The centred one stops three days early: on 26 February, center=True averages 23 February to 1 March, and the last three days in the data haven't got three days after them yet.

The second cell proves what's going on. It changes only the last day's sales to 200 kg, and the centred average for 26 February, three days earlier, jumps from 49.2 to 71.6. A value "for 26 February" depends on what happens on the 29th.

Centred averages are fine for describing the past, as a smooth line on a chart of last year. They're wrong for anything that predicts or decides: a model trained on them has seen the answers, so it looks far better than it will ever be on real days. For forecasting, use the default trailing window, which only looks back.

Check what you learned

You have daily sales and want monthly totals. What do you write?
What is s.diff() on a daily series?
Why shouldn’t a forecast use rolling(7, center=True).mean()?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis