Module 7: Time series · Lesson 7.5 · about 15 min
Weekdays, months and fiscal periods
Pull calendar features out of dates with the .dt accessor: weekday names, weekends, month, quarter, week numbers, fiscal years that start in another month, and a sales-by-weekday table in the right order.
Module 7 · about 15 minutes · uses Lesson 3.5. You’ll be able to slice sales by weekday, weekend, month and fiscal period.
In everyday terms: a calendar page: the day name, the month and the week number are all printed on it. You do not need to work them out, just read them.
1. The .dt accessor
A date column hides many useful columns. .dt pulls them out:
Think of the date as a label on a calendar page: the weekday, the month and the week number are all printed on the same page. .dt reads them off.
2. Sales by weekday, in the right order
Two habits here:
- An ordered category keeps Monday first instead of alphabetical order.
- Divide by the number of each weekday in the data. This data is exactly 78 weeks long, so every weekday appears 78 times, but most stretches of dates are not: a month can hold five Saturdays and four Mondays, so totals alone are not a fair comparison.
3. Fiscal years and periods
Many companies start their year in another month (1 April in India, Canada’s federal government and the UK tax year; 1 February for many retailers). A period with an anchor does this in one line:
'Q-MAR' means “quarters of a year that ends in March”: April to June is Q1 of the year that ends next March. Check one date by hand whenever you set this up.
- .dt.day_name() / .dt.dayofweek
- Monday ... Sunday / 0 ... 6.
- .dt.dayofweek >= 5
- True on Saturday and Sunday.
- .dt.month, .dt.quarter, .dt.isocalendar().week
- Calendar parts as numbers.
- .dt.to_period('Q-MAR').dt.qyear
- Fiscal year ending in March, labelled by the year it ends.
- Physical meaning
- Calendar features turn a date into categories you can compare. A month has 4 or 5 of each weekday, so compare weekdays by average per day, not by total.
“Sort the weekday names and you are done.” Text sorts alphabetically: Friday, Monday, Saturday… Group on dayofweek (0-6) or make the names an ordered category.
Watch it happen, one step at a time:
| date |
|---|
| 2026-03-01 |
| 2026-03-04 |
| 2026-04-02 |
.dt.day_name() → Sunday, Wednesday, Thursday; .dt.dayofweek → 6, 2, 3..dt.dayofweek >= 5 → True, False, False..dt.to_period('Q-MAR').dt.qyear → 2026, 2026, 2027.4. Your turn
How it works
Principle: a date is a point on several cycles at once: the week (7 days), the month, the quarter and the year. Each .dt feature names the position on one cycle, so you can group by it and see that cycle’s pattern.
Fair comparison: when you compare weekdays or months, compare per day, not totals, because the number of Mondays or the number of days in a month differs:
Week numbers: ISO weeks start on Monday, and week 1 is the week with the year’s first Thursday, so 1 January can fall in week 52 or 53 of the year before. Use isocalendar() for both the ISO year and the week together.
5. Practice
- Which month of 2025 had the most units per day?
- Label each date as “Q1”, “Q2”… of a fiscal year starting 1 February.
- Why might 2026-01-01 be in ISO week 1, but 2027-01-01 be in week 53?
Answers
d = orders.groupby('date')['qty'].sum().loc['2025']; d.resample('MS').mean().idxmax()(resample on a daily series averages per day).'Q' + orders['date'].dt.to_period('Q-JAN').dt.quarter.astype(str).- ISO week 1 holds the first Thursday. 1 Jan 2026 is a Thursday, so it starts week 1; 1 Jan 2027 is a Friday, so it still belongs to the last week of 2026.
New words: .dt accessor, day of week, ordered category, fiscal year, anchored period, ISO week.
- .dt reads weekday, month, quarter and week from a datetime column.
- Weekend: dayofweek >= 5.
- Order weekdays by number, not by name.
- Fiscal years: to_period(‘Q-MAR’) for a year ending in March.
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.