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 withshift,diffandpct_change - Find and fill missing days with
asfreq - Explain why
center=Truecan’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:
- A Series whose index is dates (a
DatetimeIndex) knows about time.pd.date_rangemade one here; with real data,set_index("date")afterparse_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.
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.
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
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.
- pandas: Time series / date functionality, resampling: resample, and the rule names W, MS and D
- pandas: Windowing operations: rolling windows, and what center does
- pandas: pandas.DataFrame.asfreq: a row for every period, with gaps shown
- pandas: pandas.Series.pct_change: the change from one period to the next, as a share