Pandas | to_datetime method
Start your free 7-days trial now!
Pandas to_datetime(~)
method converts the argument into a datetime
object.
Parameters
1. arg
link | number
or string
or datetime
or sequence
or map
The object to convert into a datetime
object.
2. errors
| string
| optional
How to handle cases when conversion is not successful:
Value | Description |
---|---|
| Raise an error. |
|
|
| Return the input. |
By default, errors="raise"
.
3. dayfirst
| boolean
| optional
If True
, then treat the first number as a day. For instance, "10/12/2020"
will be parsed as December 10th, 2020. By default, dayfirst=False
.
4. yearfirst
| boolean
| optional
If True
, then treat the first number as a year. For instance, "20/12/10"
will be parsed as December 10th, 2020. By default, yearfirst=False
.
5. utc
| boolean
| optional
Whether or not to set the timezone to UTC. By default, utc=False
.
6. format
| string
| optional
The format string for dates, which follows the standard Python syntax (e.g. "%d/%m/%Y"
).
7. exact
| boolean
| optional
Whether or not to enforce exact match for the specified format
. By default, exact=True
.
8. unit
| string
| optional
The time unit of the argument:
"D", "s", "ms", "us", "ns"
By default, unit="ns"
.
9. infer_datetime_format
| boolean
| optional
If format
is not specified and this parameter is set to True
, then infer the format if possible. If format can be inferred, then the dates will be parsed more efficiently.
By default, infer_datetime_format=False
.
10. origin
| scalar
| optional
The reference date to use:
"unix"
: use 1970-01-01 as the reference date"julian"
: use the start of the Julian Calendar as the reference date
By default, origin="unix"
.
11. cache
| boolean
| optional
Whether or not to leverage caching when parsing dates. Using cache will speed up the process of parsing duplicate dates, particularly those with timezone offsets. Note that caching will only take effect when the number of dates to be parsed is at least 50. By default, cache=False
.
Return Value
The return type depends on the type of arg
:
array-like
:DatetimeIndex
is returned.Series
:Series
of typedatetime64
is returned.scalar
:Timestamp
is returned.
Examples
Timestamp
To convert a date string of format MM/DD/YYYY
to Timestamp
:
pd.to_datetime("10/12/2020") # October
Timestamp('2020-10-12 00:00:00')
Notice how Pandas officially use YYYY-MM-DD
for dates.
The date string can also be in the format YYYY/MM/DD
as well:
pd.to_datetime("2020/12/10") # December
Timestamp('2020-12-10 00:00:00')
It is easy to confuse the days and months, so a good practice would be to specify the format
parameter:
pd.to_datetime("10/12/2020", format="%d/%m/%Y")
Timestamp('2020-12-10 00:00:00')
Datetime64
To convert a Series of date strings to a Series of dtype datetime64[ns]
:
0 2020-12-251 2020-12-26dtype: datetime64[ns]
DatetimeIndex
To convert an array of date strings to DatetimeIndex
, which can be used as the index of a DataFrame, pass in an array like so:
pd.to_datetime(["25/12/2020", "26/12/2020"])
DatetimeIndex(['2020-12-25', '2020-12-26'], dtype='datetime64[ns]', freq=None)