Examples
Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations
like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or plurals of the same

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'year': [2018, 2019],
                   'month': [3, 4],
                   'day': [5, 6]})
In [3]:
pd.to_datetime(df)
Out[3]:
0   2018-03-05
1   2019-04-06
dtype: datetime64[ns]

If a date does not meet the timestamp limitations, passing errors=’ignore’ will return the original input instead
of raising any exception.

Passing errors=’coerce’ will force an out-of-bounds date to NaT, in addition to forcing non-dates
(or non-parseable dates) to NaT.

In [4]:
pd.to_datetime('16000202', format='%Y%m%d', errors='ignore')
Out[4]:
datetime.datetime(1600, 2, 2, 0, 0)
In [5]:
pd.to_datetime('16000202', format='%Y%m%d', errors='coerce')
Out[5]:
NaT

Passing infer_datetime_format=True can often-times speedup a parsing if its not an ISO8601 format exactly,
but in a regular format.

In [6]:
s = pd.Series(['4/11/2017', '4/12/2017', '4/13/2017'] * 1000)
s.head()
Out[6]:
0    4/11/2017
1    4/12/2017
2    4/13/2017
3    4/11/2017
4    4/12/2017
dtype: object
In [7]:
%timeit pd.to_datetime(s,infer_datetime_format=True)  # doctest: +SKIP
5.74 ms ± 720 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [8]:
%timeit pd.to_datetime(s,infer_datetime_format=False)  # doctest: +SKIP
1.67 ms ± 83.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Using a unix epoch time

In [9]:
pd.to_datetime(1590196004, unit='s')
Out[9]:
Timestamp('2020-05-23 01:06:44')
In [10]:
pd.to_datetime(1590196004433502010, unit='ns')
Out[10]:
Timestamp('2020-05-23 01:06:44.433502010')

Using a non-unix epoch origin

In [11]:
pd.to_datetime([2, 3, 4], unit='D',
               origin=pd.Timestamp('2002-02-02'))
Out[11]:
DatetimeIndex(['2002-02-04', '2002-02-05', '2002-02-06'], dtype='datetime64[ns]', freq=None)