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
import numpy as np
import pandas as pd
df = pd.DataFrame({'year': [2018, 2019],
'month': [3, 4],
'day': [5, 6]})
pd.to_datetime(df)
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.
pd.to_datetime('16000202', format='%Y%m%d', errors='ignore')
pd.to_datetime('16000202', format='%Y%m%d', errors='coerce')
Passing infer_datetime_format=True can often-times speedup a parsing if its not an ISO8601 format exactly,
but in a regular format.
s = pd.Series(['4/11/2017', '4/12/2017', '4/13/2017'] * 1000)
s.head()
%timeit pd.to_datetime(s,infer_datetime_format=True) # doctest: +SKIP
%timeit pd.to_datetime(s,infer_datetime_format=False) # doctest: +SKIP
Using a unix epoch time
pd.to_datetime(1590196004, unit='s')
pd.to_datetime(1590196004433502010, unit='ns')
Using a non-unix epoch origin
pd.to_datetime([2, 3, 4], unit='D',
origin=pd.Timestamp('2002-02-02'))