All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 7 · about 15 minutes · uses Lesson 3.5. You’ll be able to slice sales by weekday, weekend, month and fiscal period.

Every date carries a weekday, a month, a quarter and a fiscal period. .dt reads them off, so you can ask "do weekends sell more?" with one groupby.

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:

Pythonorders.csv

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

Pythonorders.csv

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:

Pythonorders.csv

'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:

Dates.
date
2026-03-01
2026-03-04
2026-04-02
Weekday. .dt.day_name() → Sunday, Wednesday, Thursday; .dt.dayofweek → 6, 2, 3.
Weekend. .dt.dayofweek >= 5 → True, False, False.
Fiscal year starting 1 April. .dt.to_period('Q-MAR').dt.qyear → 2026, 2026, 2027.
Why 2027? 2 April 2026 is in the fiscal year that ends in March 2027.

4. Your turn

Your turnorders.csv
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:

per dayw=totalwnumber of days of type w

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

  1. Which month of 2025 had the most units per day?
  2. Label each date as “Q1”, “Q2”… of a fiscal year starting 1 February.
  3. Why might 2026-01-01 be in ISO week 1, but 2027-01-01 be in week 53?
Answers
  1. d = orders.groupby('date')['qty'].sum().loc['2025']; d.resample('MS').mean().idxmax() (resample on a daily series averages per day).
  2. 'Q' + orders['date'].dt.to_period('Q-JAN').dt.quarter.astype(str).
  3. 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

1What does orders['date'].dt.dayofweek give for a Monday?
2Why do weekday names come out in alphabetical order after a groupby?
3A fiscal year starts on 1 April. Which fiscal year is 15 February 2026 in?

Sources

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.