Parse dates
Parse dates — parseDates
Parse text columns into real datetimes so date operations (sorting, Extract date parts) work. Complements Extract date parts (which goes the other way: datetime → parts).
Use cases
- Convert
"2021-01-02"strings into datetimes before sorting or grouping by month. - Clean a messy date column, sending unparseable values to null.
What it does
Converts each matching text value to a datetime. Unparseable strings become null
when errors=coerce (the default).
Before
| id | ordered_at |
|---|---|
| 1 | 2024-01-15 |
| 2 | 2024-02-20 |
| 3 | bad date |
3 rows · 2 cols
Parse dates (columns=[ordered_at], errors=coerce)
After
| id | ordered_atnew |
|---|---|
| 1 | 2024-01-15 00:00:00 |
| 2 | 2024-02-20 00:00:00 |
| 3 | null |
3 rows · 2 cols
Configuration
| Config key | Type | Required | Description |
|---|---|---|---|
columns | string[] | Yes | Text columns to parse |
format | string | No | strptime format (e.g. %d-%m-%Y); empty = auto-detect |
errors | string | No | coerce (default, bad values → null) or raise |
Generated Python code
df_2 = df_1.assign(ordered_at=lambda _d: pd.to_datetime(_d['ordered_at'], errors='coerce'))
Tips & common mistakes
- Give a
formatfor ambiguous dates (e.g.%d-%m-%Yvs%m-%d-%Y) so day and month aren't swapped. coerceis the safe default — it won't fail a run on one bad value. Useraisewhen you want to catch unexpected formats early.