pandas.to_datetime¶
- pandas.to_datetime(arg, errors='ignore', dayfirst=False, utc=None, box=True, format=None, coerce=False, unit='ns', infer_datetime_format=False)¶
Convert argument to datetime
Parameters : arg : string, datetime, array of strings (with possible NAs)
errors : {‘ignore’, ‘raise’}, default ‘ignore’
Errors are ignored by default (values left untouched)
dayfirst : boolean, default False
If True parses dates with the day first, eg 20/01/2005 Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug).
utc : boolean, default None
Return UTC DatetimeIndex if True (converting any tz-aware datetime.datetime objects as well)
box : boolean, default True
If True returns a DatetimeIndex, if False returns ndarray of values
format : string, default None
strftime to parse time, eg “%d/%m/%Y”
coerce : force errors to NaT (False by default)
unit : unit of the arg (D,s,ms,us,ns) denote the unit in epoch
(e.g. a unix timestamp), which is an integer/float number
infer_datetime_format: boolean, default False
If no format is given, try to infer the format based on the first datetime string. Provides a large speed-up in many cases.
Returns : ret : datetime if parsing succeeded
Examples
Take separate series and convert to datetime
>>> import pandas as pd >>> i = pd.date_range('20000101',periods=100) >>> df = pd.DataFrame(dict(year = i.year, month = i.month, day = i.day)) >>> pd.to_datetime(df.year*10000 + df.month*100 + df.day, format='%Y%m%d')
Or from strings
>>> df = df.astype(str) >>> pd.to_datetime(df.day + df.month + df.year, format="%d%m%Y")