Module 7: Time series · Lesson 7.1 · about 15 min
Dates as the index
Build a daily time series from transactions, set the date as the index, select by date strings and ranges, and fill in missing days so the series has one row for every day.
Module 7 · about 15 minutes · uses Lessons 3.5 and 4.1. You’ll be able to turn transactions into a clean daily series indexed by date.
In everyday terms: a diary with a page for every day. If you only write on busy days, flicking through makes the month look busier than it was; blank pages are part of the story.
1. From transactions to a daily series
The index is a DatetimeIndex, which unlocks date selection, resampling and rolling windows.
2. Selecting by date
Partial strings ('2026', '2026-03') select whole periods, and date slices include both ends.
3. Every day present
A store with no sales on a day simply has no rows that day. Before rolling windows or growth rates, put the missing days back as zeros:
reindex lines the series up with the full calendar, filling the gaps.
- groupby('date')['revenue'].sum()
- Transactions to one row per day.
- set_index('date')
- Makes the dates the row labels, so date tools work.
- daily.loc['2026-03']
- Selects a whole month by a partial date.
- asfreq('D', fill_value=0)
- Adds the missing days, with 0 where nothing was sold.
- Physical meaning
- Each row is a slice of time, and the gaps are real days with zero sales. Without them, a 30-day month can look like 26 busy days, and averages and rolling windows are computed over the wrong span.
“Days with no sales do not need a row.” Leaving them out inflates the daily average and makes a 7-row rolling window cover more than 7 days. Add them with asfreq and decide if the value is 0 or unknown.
Watch it happen, one step at a time:
| date | revenue |
|---|---|
| 2026-03-01 | 40 |
| 2026-03-01 | 25 |
| 2026-03-03 | 30 |
groupby('date')['revenue'].sum()| date | revenue |
|---|---|
| 2026-03-01 | 65 |
| 2026-03-03 | 30 |
.asfreq('D', fill_value=0)| date | revenue |
|---|---|
| 2026-03-01 | 65 |
| 2026-03-02 | 0 |
| 2026-03-03 | 30 |
4. Your turn
How it works
Principle: time-series tools assume a regular series: one row per period, in order. Transactions are irregular (many on busy days, none on quiet ones). groupby + reindex (or resample, next lesson) converts one to the other.
Zero vs missing: a day with no orders had zero sales, so fill with 0. A day with no sensor reading is unknown, so leave NaN or interpolate. Decide which your data means.
5. Practice
- Select all of 2025 from a daily series.
- Why might a “7-row rolling average” not be a 7-day average?
- Make a calendar of every Monday in 2026.
Answers
daily.loc['2025'].- If quiet days are missing, 7 rows can span more than 7 days; reindex to every day first (or use
rolling('7D')). pd.date_range('2026-01-01', '2026-12-31', freq='W-MON').
New words: time series, DatetimeIndex, date_range, reindex, regular series.
- Group transactions to one row per day, then set the date as index.
- Partial dates select ranges: loc[‘2026-03’].
- asfreq(‘D’, fill_value=0) adds missing days.
- Averages and windows need every day present.
Check yourself
Sources
- pandas documentation, User Guide: Time series / date functionality https://pandas.pydata.org/docs/user_guide/timeseries.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.