Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

pd.to_datetime

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.

How it works

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.

Syntax

pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=False, format=None, exact=<no_default>, unit=None, origin='unix', cache=True)

Parameters

NameTypeDescription
argint, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-likeValue 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.
dayfirstbool, default FalseInterpret ambiguous dates with the day before the month.
yearfirstbool, default FalseInterpret ambiguous dates with the year first.
utcbool, default FalseConvert results to timezone-aware UTC values.
formatstr, default Nonestrftime pattern used to parse strings.
exactbool, default <no_default>Require the complete format to match when a format is supplied.
unitstr, default NoneUnit for numeric timestamps, such as D, s, ms, us, or ns.
originscalar, default 'unix'Reference point for numeric timestamps.
cachebool, default TrueUse a cache for unique converted date strings.

Returns

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.

Key points

  • It provides one conversion interface for individual values, collections, Series, and table-like data.
  • The required arg parameter may be an integer, float, string, datetime, list, tuple, one-dimensional array, Series, or DataFrame/dict-like object.
  • format accepts a strftime pattern so parsing can match a known string layout.
  • utc=True produces timezone-aware UTC output, including localization of timezone-naive values.
  • Caching is enabled by default and can improve conversion when inputs contain repeated date strings.

Common mistakes

  • With the default errors='raise', an invalid date string raises a ParserError; choose errors='coerce' when invalid values should become missing timestamps.
  • dayfirst influences ambiguous numeric dates but is not a strict requirement, so explicitly provide format when the input layout must be enforced.
  • Setting utc=True normalizes timezone-aware inputs to UTC and localizes timezone-naive inputs to UTC.

Example: Parse dates as UTC

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.

When to use it

Normalize date columns

A column contains date strings and needs datetime operations such as sorting, filtering, or extracting components.

Decode numeric timestamps

A dataset stores Unix timestamps as integers or floats.

Coerce invalid dates

Inputs may contain malformed date strings that should not stop a pipeline.

Normalize timestamps to UTC

Data from multiple sources uses mixed timezone conventions.

Interview questions

QWhat inputs does pandas.to_datetime support, and what does it return?

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.

QHow can parsing failures be handled?

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.

QHow do timezone and numeric timestamp options affect conversion?

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.Timestamp
  • pandas.DatetimeIndex
  • pandas.to_timedelta
  • pandas.date_range
  • Series.dt

Source: pandas.to_datetime. Checked 2026-10-15.