search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | to_datetime method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas to_datetime(~) method converts the argument into a datetime object.

Parameters

1. arglink | 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"

Raise an error.

"coerce"

NaT is returned.

"ignore"

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 type datetime64 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]:

pd.to_datetime(pd.Series(["25/12/2020", "26/12/2020"]))
0 2020-12-25
1 2020-12-26
dtype: 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)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!