pandas.to_datetime¶
- pandas.to_datetime(*args, **kwargs)¶
- Convert argument to datetime. - Parameters: - arg : string, datetime, array of strings (with possible NAs) - errors : {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ - If ‘raise’, then invalid parsing will raise an exception
- If ‘coerce’, then invalid parsing will be set as NaT
- If ‘ignore’, then invalid parsing will return the input
 - dayfirst : boolean, default False - Specify a date parse order if arg is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior). - yearfirst : boolean, default False - Specify a date parse order if arg is str or its list-likes. - If True parses dates with the year first, eg 10/11/12 is parsed as 2010-11-12. - If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). Warning: yearfirst=True is not strict, but will prefer to parse with year first (this is a known bug, based on dateutil beahavior). - 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”, note that “%f” will parse all the way up to nanoseconds. - exact : boolean, True by default - If True, require an exact format match.
- If False, allow the format to match anywhere in the target string.
 - 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. - Return type depends on input: - list-like: DatetimeIndex
- Series: Series of datetime64 dtype
- scalar: Timestamp
 - In case when it is not possible to return designated types (e.g. when any element of input is before Timestamp.min or after Timestamp.max) return will have datetime.datetime type (or correspoding array/Series). - 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') 0 2000-01-01 1 2000-01-02 ... 98 2000-04-08 99 2000-04-09 Length: 100, dtype: datetime64[ns] - Or from strings - >>> df = df.astype(str) >>> pd.to_datetime(df.day + df.month + df.year, format="%d%m%Y") 0 2000-01-01 1 2000-01-02 ... 98 2000-04-08 99 2000-04-09 Length: 100, dtype: datetime64[ns] - Date that does not meet timestamp limitations: - >>> pd.to_datetime('13000101', format='%Y%m%d') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT