All lessons

Module 3: Cleaning data · Lesson 3.5 · about 20 min

Dates and times

Turn text into real dates with to_datetime, handle mixed formats, pull out year, month and weekday with .dt, do date arithmetic with Timedelta, and filter by date ranges.

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

Module 3 · about 20 minutes · uses Lesson 3.3. You’ll be able to read dates in any format, take them apart, and calculate with them.

Turn date text into real dates once, then pandas can sort, filter, take dates apart and do date arithmetic correctly.

In everyday terms: “03/04/2026” is the 3rd of April in Canada’s DD/MM habit and March 4th in the US. A real date type removes the guesswork, like writing 2026-04-03.

1. Text to dates

Pythonorders.csv

For other layouts, give the format: %Y year, %m month, %d day, %b month name (Jan), %H:%M time.

2. Mixed formats

The messy export mixes 2025-07-04, 04/07/2025 (day first) and Jul 04 2025. Parse each known format, then combine:

Pythonorders_messy.csv

Each errors='coerce' turns values in other formats into NaT; fillna takes the first that worked. This is safer than letting pandas guess, which can swap day and month on dates like 04/07/2025.

3. Taking dates apart

Pythonorders.csv

4. Date arithmetic

Pythoncustomers.csv

pd.Timedelta(days=30) is a fixed length; pd.DateOffset(months=1) follows the calendar (Jan 31 + 1 month = Feb 28).

pd.to_datetime(col)
Text to datetime64.
format= / dayfirst=
Say how ambiguous dates are written.
.dt.year, .dt.month, .dt.day_name()
Parts of each date.
date2 − date1
A Timedelta; .dt.days gives whole days.
Physical meaning
A real date is a point on a timeline, so subtracting two gives a duration and sorting follows time. As text, "2026-10-02" and "2026-9-30" sort the wrong way round.

“pandas will figure out the format.” With mixed formats it may guess day-first for some rows and month-first for others. Give a format, or parse each format separately, and check min and max afterwards.

Watch it happen, one step at a time:

Text. ['2026-03-01', '2026-03-15', '2026-04-02'] (object).
Convert. pd.to_datetime(df['date']) → datetime64.
Take apart. .dt.month → 3, 3, 4; .dt.day_name() → Sunday, Sunday, Thursday.
Arithmetic. (df['date'] - df['date'].min()).dt.days → 0, 14, 32.

5. Your turn

Your turnorders.csv
Your turn
How it works

Principle: a datetime is stored as a number: nanoseconds since 1 January 1970. That’s why dates sort, subtract and compare correctly, and why .dt can compute the weekday or month instantly for millions of rows.

Time zones: dates without a zone are “naive”. For data from several zones, convert with .dt.tz_localize('America/Toronto') and .dt.tz_convert('UTC') before comparing.

6. Practice

  1. Keep orders from March 2026.
  2. Number of days between each customer’s signup and their first order (merge comes in Module 5; describe the steps).
  3. Why is format= safer than letting pandas guess?
Answers
  1. orders[(orders['date'] >= '2026-03-01') & (orders['date'] < '2026-04-01')] or orders[orders['date'].dt.to_period('M') == '2026-03'].
  2. Find each customer’s first order date (groupby('customer_id')['date'].min()), join it to customers, subtract signup_date, take .dt.days.
  3. Guessing can read 04/07/2025 as April 7 or 4 July depending on the rest of the column; an explicit format is never ambiguous.

Try it on your own data: the timestamp converter tool turns timestamps into readable dates.

New words: datetime, to_datetime, format codes, NaT, .dt accessor, Timedelta, DateOffset, period.

  • Convert date text with pd.to_datetime (or parse_dates when reading).
  • State the format when dates are ambiguous or mixed.
  • .dt gives parts: year, month, day_name, quarter, to_period.
  • Subtracting dates gives Timedeltas; .dt.days gives whole days.

Check yourself

1Why convert a date column from text to datetime?
2How do you get the weekday name from a datetime column?
3What is (date2 − date1).dt.days?

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.