Data Cleaning Exercises

Module 7 · Dates and time · 6 min

Impossible dates

You'll be able to: Quarantine dates that don't exist instead of letting a parser roll them over.

Before this: Dates need a declared locale

The idea

2026-02-30 and 31/04/2026 don't exist. Lenient parsers quietly turn them into March 2 or May 1.

Use strict parsing and treat a failed parse as an invalid_date exception, separately from missing values.

Check leap years: 2028-02-29 is valid, 2026-02-29 is not.

Worked example

Input: raw_date: '2026-02-30', '2028-02-29'

Visual
Add step → Dates by locale → invalid days quarantined as invalid_date
SQL (DuckDB)
TRY_STRPTIME(raw_date, '%Y-%m-%d')  -- NULL for 2026-02-30
pandas
pd.to_datetime(s, format="%Y-%m-%d", errors="coerce")

Result: 2026-02-30 → invalid_date; 2028-02-29 is valid

Common mistakes

  • Lenient rollover.
  • Mixing invalid and missing.
  • Forgetting leap years.

Check yourself

Is 2026-02-29 valid?

No; 2026 is not a leap year.

Practice it

Parse dates by declared locale.

Next: Parse dates by declared locale