Change types
Change types — castDtypes
Convert column data types.
Use cases
- Turn numbers read as text (
"42") into real integers/floats for math. - Parse a date column into a real
datetimeso sorting and Extract date parts work. - Coerce a dirty column, sending unparseable values to null instead of failing.
What it does
Rewrites the column values to the target type. With errors: coerce, rows with
unparseable values become null rather than crashing the run.
Before
| order_id | amount | ordered_at |
|---|---|---|
| 1001 | 120.5 | 2024-01-03 |
| 1002 | bad | 2024-01-04 |
| 1003 | 89.0 | 2024-01-05 |
3 rows · 3 cols
Change types (amount→float errors=coerce, ordered_at→datetime)
After
| order_id | amount | ordered_at |
|---|---|---|
| 1001 | 120.5 | 2024-01-03 |
| 1002 | null | 2024-01-04 |
| 1003 | 89 | 2024-01-05 |
3 rows · 3 cols
Configuration
| Config key | Type | Required | Description |
|---|---|---|---|
casts | object | Yes | { "col": "dtype" } — dtype is integer, float, boolean, string, or datetime |
format | string | No | datetime parse format (e.g. %Y-%m-%d) |
errors | string | No | raise (default) or coerce (invalid → null) |
Generated Python code
df_2 = df_1.assign(amount=lambda _d: _d['amount'].astype('float64'), ordered_at=lambda _d: pd.to_datetime(_d['ordered_at']))
Tips & common mistakes
- Use
coercefor dirty data. Witherrors: raise(the default), a single unparseable value fails the whole run;coerceturns bad values into null. - Give datetimes a
formatwhen the layout is unambiguous (e.g.%m-%Y) — it's faster and avoids mis-parsing day/month order. - For text-to-date parsing across several columns at once, see Parse dates.