Normalize date columns
A column contains date strings and needs datetime operations such as sorting, filtering, or extracting components.
Pandas · Function
Converts scalar, array-like, Series, or DataFrame/dict-like input into pandas datetime objects. Options control error handling, parsing formats, timezone normalization, numeric units, origin, and caching.
Use pd.to_datetime to convert scalar values, sequences, Series, or table-like data into pandas datetime objects. Parsing behavior can be controlled with format, errors, timezone normalization through utc, units for numeric values, and origin for numeric timestamps.
pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=False, format=None, exact=<no_default>, unit=None, origin='unix', cache=True)| Name | Type | Description |
|---|---|---|
arg | int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like | Value or collection to convert; supported inputs include numeric and textual values, datetime objects, one-dimensional arrays, Series, and DataFrame/dict-like data. |
errors | {'raise', 'coerce', 'ignore'}, default 'raise' | Controls failures: raise an exception, coerce invalid values to NaT, or ignore conversion. |
dayfirst | bool, default False | Interpret ambiguous dates with the day before the month. |
yearfirst | bool, default False | Interpret ambiguous dates with the year first. |
utc | bool, default False | Convert results to timezone-aware UTC values. |
format | str, default None | strftime pattern used to parse strings. |
exact | bool, default <no_default> | Require the complete format to match when a format is supplied. |
unit | str, default None | Unit for numeric timestamps, such as D, s, ms, us, or ns. |
origin | scalar, default 'unix' | Reference point for numeric timestamps. |
cache | bool, default True | Use a cache for unique converted date strings. |
The return type depends on the input: scalar input returns Timestamp, array-like input returns DatetimeIndex, Series input returns Series, and DataFrame input returns Series; Python datetime or object-dtype fallbacks can occur for unsuccessful timezone or out-of-range parsing.
import pandas as pd
values = pd.to_datetime(
["2024-01-15", "2024-02-20"],
format="%Y-%m-%d",
utc=True,
)
print(values)Converts ISO date strings into a UTC-aware DatetimeIndex.
A column contains date strings and needs datetime operations such as sorting, filtering, or extracting components.
A dataset stores Unix timestamps as integers or floats.
Inputs may contain malformed date strings that should not stop a pipeline.
Data from multiple sources uses mixed timezone conventions.
The function accepts scalars, array-like values, Series, and DataFrame or dict-like inputs. The result type follows the input: a scalar produces a Timestamp, an array-like produces a DatetimeIndex, and a Series produces a Series.
Follow-up: What happens for a DataFrame? It must provide at least year, month, and day columns, and the result is a Series of datetime values.
errors='raise' propagates parsing failures, errors='coerce' turns invalid values into NaT, and errors='ignore' leaves the input unchanged when parsing fails. The default is raise.
Follow-up: How would you make parsing deterministic for ambiguous strings? Supply an explicit strftime format rather than relying on inference.
utc=True makes the output timezone-aware and UTC-normalized. Numeric inputs can be interpreted using a unit such as seconds or milliseconds and an origin, whose default is Unix time.
Follow-up: Why specify unit for integers? Without it, numeric timestamps are not given the intended time scale; unit identifies the timestamp resolution.
pandas.Timestamppandas.DatetimeIndexpandas.to_timedeltapandas.date_rangeSeries.dt