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.
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.
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
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:
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
4. Date arithmetic
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:
pd.to_datetime(df['date']) → datetime64..dt.month → 3, 3, 4; .dt.day_name() → Sunday, Sunday, Thursday.(df['date'] - df['date'].min()).dt.days → 0, 14, 32.5. 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
- Keep orders from March 2026.
- Number of days between each customer’s signup and their first order (merge comes in Module 5; describe the steps).
- Why is
format=safer than letting pandas guess?
Answers
orders[(orders['date'] >= '2026-03-01') & (orders['date'] < '2026-04-01')]ororders[orders['date'].dt.to_period('M') == '2026-03'].- Find each customer’s first order date (
groupby('customer_id')['date'].min()), join it to customers, subtractsignup_date, take.dt.days. - Guessing can read
04/07/2025as 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
Sources
- pandas documentation, User Guide: Time series / date functionality https://pandas.pydata.org/docs/user_guide/timeseries.html
- ISO 8601 Date and time format https://www.iso.org/iso-8601-date-and-time-format.html
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.