- pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=False, format=None, exact=<no_default>, unit=None, infer_datetime_format=<no_default>, origin='unix', cache=True)[source]#
Convert argument to datetime.
This function converts a scalar, array-like, Series orDataFrame/dict-like to a pandas datetime object.
- Parameters:
- argint, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like
The object to convert to a datetime. If a DataFrame is provided, themethod expects minimally the following columns:
"year"
,"month"
,"day"
. The column “year”must be specified in 4-digit format.- 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.
- dayfirstbool, default False
Specify a date parse order if arg is str or is list-like.If
True
, parses dates with the day first, e.g."10/11/12"
is parsed as2012-11-10
.Warning
dayfirst=True
is not strict, but will prefer to parsewith day first.- yearfirstbool, default False
Specify a date parse order if arg is str or is list-like.
If
True
parses dates with the year first, e.g."10/11/12"
is parsed as2010-11-12
.If both dayfirst and yearfirst are
True
, yearfirst ispreceded (same asdateutil
).
Warning
yearfirst=True
is not strict, but will prefer to parsewith year first.- utcbool, default False
Control timezone-related parsing, localization and conversion.
If
True
, the function always returns a timezone-awareUTC-localized Timestamp, Series orDatetimeIndex. To do this, timezone-naive inputs arelocalized as UTC, while timezone-aware inputs are converted to UTC.If
False
(default), inputs will not be coerced to UTC.Timezone-naive inputs will remain naive, while timezone-aware oneswill keep their time offsets. Limitations exist for mixedoffsets (typically, daylight savings), see Examples section for details.
Warning
In a future version of pandas, parsing datetimes with mixed timezones will raise an error unless utc=True.Please specify utc=True to opt in to the new behaviourand silence this warning. To create a Series with mixed offsets andobject dtype, please use apply and datetime.datetime.strptime.
See also: pandas general documentation about timezone conversion andlocalization.
- formatstr, default None
The strftime to parse time, e.g.
"%d/%m/%Y"
. Seestrftime documentation for more information on choices, thoughnote that"%f"
will parse all the way up to nanoseconds.You can also pass:“ISO8601”, to parse any ISO8601time string (not necessarily in exactly the same format);
“mixed”, to infer the format for each element individually. This is risky,and you should probably use it along with dayfirst.
Note
See AlsoPython/Pandas: How to convert from datetime64[ns] to datetimeDatetimes and timedeltas — NumPy v2.1 Manualpandas.to_datetime — pandas 1.5.2 documentationTime series / date functionality — pandas 2.2.3 documentationIf a DataFrame is passed, then format has no effect.
- exactbool, default True
Control how format is used:
If
True
, require an exact format match.If
False
, allow the format to match anywhere in the targetstring.
Cannot be used alongside
format='ISO8601'
orformat='mixed'
.- unitstr, default ‘ns’
The unit of the arg (D,s,ms,us,ns) denote the unit, which is aninteger or float number. This will be based off the origin.Example, with
unit='ms'
andorigin='unix'
, this would calculatethe number of milliseconds to the unix epoch start.- infer_datetime_formatbool, default False
If
True
and no format is given, attempt to infer the formatof the datetime strings based on the first non-NaN element,and if it can be inferred, switch to a faster method of parsing them.In some cases this can increase the parsing speed by ~5-10x.Deprecated since version 2.0.0: A strict version of this argument is now the default, passing it hasno effect.
- originscalar, default ‘unix’
Define the reference date. The numeric values would be parsed as numberof units (defined by unit) since this reference date.
If
'unix'
(or POSIX) time; origin is set to 1970-01-01.If
'julian'
, unit must be'D'
, and origin is set tobeginning of Julian Calendar. Julian day number0
is assignedto the day starting at noon on January 1, 4713 BC.If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or datestring), origin is set to Timestamp identified by origin.
If a float or integer, origin is the difference(in units determined by the
unit
argument) relative to 1970-01-01.
- cachebool, default True
If
True
, use a cache of unique, converted dates to apply thedatetime conversion. May produce significant speed-up when parsingduplicate date strings, especially ones with timezone offsets. The cacheis only used when there are at least 50 values. The presence ofout-of-bounds values will render the cache unusable and may slow downparsing.
- Returns:
- datetime
If parsing succeeded.Return type depends on input (types in parenthesis correspond tofallback in case of unsuccessful timezone or out-of-range timestampparsing):
scalar: Timestamp (or
datetime.datetime
)array-like: DatetimeIndex (or Series with
object
dtype containingdatetime.datetime
)Series: Series of
datetime64
dtype (orSeries ofobject
dtype containingdatetime.datetime
)DataFrame: Series of
datetime64
dtype (orSeries ofobject
dtype containingdatetime.datetime
)
- Raises:
- ParserError
When parsing a date from string fails.
- ValueError
When another datetime conversion error happens. For example when oneof ‘year’, ‘month’, day’ columns is missing in a DataFrame, orwhen a Timezone-aware
datetime.datetime
is found in an array-likeof mixed time offsets, andutc=False
.
See also
- DataFrame.astype
Cast argument to a specified dtype.
- to_timedelta
Convert argument to timedelta.
convert_dtypes
Convert dtypes.
Notes
Many input types are supported, and lead to different output types:
scalars can be int, float, str, datetime object (from stdlib
datetime
module ornumpy
). They are converted to Timestamp whenpossible, otherwise they are converted todatetime.datetime
.None/NaN/null scalars are converted to NaT.array-like can contain int, float, str, datetime objects. They areconverted to DatetimeIndex when possible, otherwise they areconverted to Index with
object
dtype, containingdatetime.datetime
. None/NaN/null entries are converted toNaT in both cases.Series are converted to Series with
datetime64
dtype when possible, otherwise they are converted to Series withobject
dtype, containingdatetime.datetime
. None/NaN/nullentries are converted to NaT in both cases.DataFrame/dict-like are converted to Series with
datetime64
dtype. For each row a datetime is created from assemblingthe various dataframe columns. Column keys can be common abbreviationslike [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) orplurals of the same.
The following causes are responsible for
datetime.datetime
objectsbeing returned (possibly inside an Index or a Series withobject
dtype) instead of a proper pandas designated type(Timestamp, DatetimeIndex or Serieswithdatetime64
dtype):when any input element is before Timestamp.min or afterTimestamp.max, see timestamp limitations.
when
utc=False
(default) and the input is an array-like orSeries containing mixed naive/aware datetime, or aware with mixedtime offsets. Note that this happens in the (quite frequent) situation whenthe timezone has a daylight savings policy. In that case you may wish touseutc=True
.
Examples
Handling various input formats
Assembling a datetime from multiple columns of a DataFrame. The keyscan be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’,‘ms’, ‘us’, ‘ns’]) or plurals of the same
>>> df = pd.DataFrame({'year': [2015, 2016],... 'month': [2, 3],... 'day': [4, 5]})>>> pd.to_datetime(df)0 2015-02-041 2016-03-05dtype: datetime64[ns]
Using a unix epoch time
>>> pd.to_datetime(1490195805, unit='s')Timestamp('2017-03-22 15:16:45')>>> pd.to_datetime(1490195805433502912, unit='ns')Timestamp('2017-03-22 15:16:45.433502912')
Warning
For float arg, precision rounding might happen. To preventunexpected behavior use a fixed-width exact type.
Using a non-unix epoch origin
>>> pd.to_datetime([1, 2, 3], unit='D',... origin=pd.Timestamp('1960-01-01'))DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)
Differences with strptime behavior
"%f"
will parse all the way up to nanoseconds.>>> pd.to_datetime('2018-10-26 12:00:00.0000000011',... format='%Y-%m-%d %H:%M:%S.%f')Timestamp('2018-10-26 12:00:00.000000001')
Non-convertible date/times
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('13000101', format='%Y%m%d', errors='coerce')NaT
Timezones and time offsets
The default behaviour (
utc=False
) is as follows:Timezone-naive inputs are converted to timezone-naive DatetimeIndex:
>>> pd.to_datetime(['2018-10-26 12:00:00', '2018-10-26 13:00:15'])DatetimeIndex(['2018-10-26 12:00:00', '2018-10-26 13:00:15'], dtype='datetime64[ns]', freq=None)
Timezone-aware inputs with constant time offset are converted totimezone-aware DatetimeIndex:
>>> pd.to_datetime(['2018-10-26 12:00 -0500', '2018-10-26 13:00 -0500'])DatetimeIndex(['2018-10-26 12:00:00-05:00', '2018-10-26 13:00:00-05:00'], dtype='datetime64[ns, UTC-05:00]', freq=None)
However, timezone-aware inputs with mixed time offsets (for exampleissued from a timezone with daylight savings, such as Europe/Paris)are not successfully converted to a DatetimeIndex.Parsing datetimes with mixed time zones will show a warning unlessutc=True. If you specify utc=False the warning below will be shownand a simple Index containing
datetime.datetime
objects will be returned:
>>> pd.to_datetime(['2020-10-25 02:00 +0200',... '2020-10-25 04:00 +0100']) FutureWarning: In a future version of pandas, parsing datetimes with mixedtime zones will raise an error unless `utc=True`. Please specify `utc=True`to opt in to the new behaviour and silence this warning. To create a `Series`with mixed offsets and `object` dtype, please use `apply` and`datetime.datetime.strptime`.Index([2020-10-25 02:00:00+02:00, 2020-10-25 04:00:00+01:00], dtype='object')
A mix of timezone-aware and timezone-naive inputs is also converted toa simple Index containing
datetime.datetime
objects:
>>> from datetime import datetime>>> pd.to_datetime(["2020-01-01 01:00:00-01:00",... datetime(2020, 1, 1, 3, 0)]) FutureWarning: In a future version of pandas, parsing datetimes with mixedtime zones will raise an error unless `utc=True`. Please specify `utc=True`to opt in to the new behaviour and silence this warning. To create a `Series`with mixed offsets and `object` dtype, please use `apply` and`datetime.datetime.strptime`.Index([2020-01-01 01:00:00-01:00, 2020-01-01 03:00:00], dtype='object')
Setting
utc=True
solves most of the above issues:Timezone-naive inputs are localized as UTC
>>> pd.to_datetime(['2018-10-26 12:00', '2018-10-26 13:00'], utc=True)DatetimeIndex(['2018-10-26 12:00:00+00:00', '2018-10-26 13:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None)
Timezone-aware inputs are converted to UTC (the output represents theexact same datetime, but viewed from the UTC time offset +00:00).
>>> pd.to_datetime(['2018-10-26 12:00 -0530', '2018-10-26 12:00 -0500'],... utc=True)DatetimeIndex(['2018-10-26 17:30:00+00:00', '2018-10-26 17:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None)
Inputs can contain both string or datetime, the aboverules still apply
>>> pd.to_datetime(['2018-10-26 12:00', datetime(2020, 1, 1, 18)], utc=True)DatetimeIndex(['2018-10-26 12:00:00+00:00', '2020-01-01 18:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None)
pandas.to_datetime — pandas 2.2.3 documentation (2024)
Top Articles
MyCare Ohio - CareSource
Introducing PRISMA: RSPO's Certification, Trade and Traceability System for Sustainable Palm Oil Management - Roundtable on Sustainable Palm Oil (RSPO)
List of Top Clinical Trial Management Systems 2024
Latest Posts
Recommended Articles
- How to Put a Diaper on Yourself: A Guide for Adults and Elderly Individuals - Diaper Factory
- A Love Story In Moist Rainy Days Theatres
- TEST: Tin Hearts - entspanntes Puzzle-Adventure für Geduldige
- Gala Bichir Color De Ojos
- Sandrine Piau - Haendel — Dijon - Forum Opéra
- Cast For Gamba ガンバと仲間たち (2015)
- Where Was The Movie Black Widows Filmed
- Neko Atsume cats - the purrfect companions
- When Is Venaitura Set
- LocoRoco Midnight Carnival Review - IGN
- HDO BOX - Best Movie App
- Estopa's Kids
- Film Poster ハクション大魔王2020
- Chii's Sweet Home Good Or Bad
- Saga of Tanya the Evil: The Movie (Anime)
- 8 Ideas For Halloween Fun With Middle School Kids
- Why Is Salsa Ni L (2024) Not On Max
- How Much Does Ivan Passer Make
- L-glutamine | Natura Foundation
- The Assistant (2024) Full Movie Online
- Is a telephone bill an utilities expense?
- Spike Chunsoft, Inc. Reveals Features for Shiren the Wanderer: The Mystery Dungeon of Serpentcoil Island
- What You Need to Know about Tattoos and Body Piercings
- Tengen Toppa Gurren Lagann
- Create Your Own Custom Embroidered Patches – THE/STUDIO
- Tv Tropes Bianca Longpré
- Proper Attire Required (1997) Actors
- De 11 beste eyeliners van 2024 | Elegantie
- PURElite Large Magnifying Clip-on Lamp (2x magnification)
- Health and environmental sanitation in India: Issues for prioritizing control strategies
- D’Nealian Handwriting
- Movies That Parminder Nagra Has Been In
- Gigantis, The Fire Monster (1959) Producer
- Kanyou Shoujo Animation Company
- The Illusionary Golden Bowl Behind The Voice Actors
- Lochlyn Munro Wife Photo
- Board Report - 10+ Examples, How to draft, Format, Word
- 孙安可 Age Difference
- Music From Corleone: A History Of La Cosa Nostra
- Food Packaging Supplies - U.S. Packaging & Wrapping
- These Eyelash Glues Will Keep Your Extensions in Place for Weeks
- The Men Of Yoshiwara: Ohgiya Seed
- Where To Watch The Game Of Truth (1961)
- Is Dive! Good On Switch
- 🌞200,000+ Young Naturist Pics, Experience the Joy of Naturism
- Hawthorn: Meaning and Symbolism - FloristEmpire
- Yoku Chapter 19
- Tokyo Yamanote Boys Pure Raspberry Disc Pool
- If You Like Dead Island Definitive Collection
- Michael Dowding Nj
- Gem of a Star: Andrea Brillantes lives up to her name
- Parisse Boothe Who Is She
- What's The First 人狼 Jin-Roh (1999) Movie
- Interview: Juno Mak | Music | Time Out Hong Kong
- B'tx Japan
- What Is The Meaning Behind Restore Point (2023)
- 10 easy ways to make your eyes look bigger instantly
- The Age Of Anger How Long
- Lee Dong Hwi - Bio, Profile, Facts, Age, Girlfriend, Net Worth
- Dr. Slump Movie 10: Arale No Bikkuriman Batch
- Big George Foreman cast: Who stars in the sports drama? (Who plays George Foreman?)
- Alison O'donnell Biggest Hits
- Destiny 2: Curse Of Osiris Error Code
- Spring, Love And You Pc Download
- What's After Lady Of Law
- Review Of Olaf Presents
- Warning (2021) Full Movie Online
- 9 Drama Korea Oh Eun Seo Terbaik Rating Tertinggi, Aktris Cilik Menggemaskan yang Jago Akting
- Sazan To Suisei No Shoujo Girls
- Island Tribe 5 Lösungshilfe zum Spiel
- Best FPS Games On PlayStation VR2, Ranked
- Where Is Hunter Playing
- Cluster Edge: Secret Episode Ch 83
- Every Movie and Series Releasing on Netflix in 2024
- Marugoto♥Anju Gakuen Manga 48
- Stellar Terminus Best Fps
- Watch Elfen Lied English Sub/Dub online Free on HiAnime.to
- Yu-Gi-Oh! Bam Pocket Wwe
- Revive ‘Calígula’ en la gloria que se le arrebató
- Lyrics containing the term: slippery slope
- City In Natsuki Crisis Battle
- The 8 Best PS5 Boxing Games
- How Many Seasons Of Is This A Zombie?
- Dog Lay Afternoon (1976)
- Roland Gervet Hacker
- Search Manga - MyAnimeList.net
- What Height Is Noah Taylor
- Simulacro Achievements Xbox
- Tanaka-kun wa Itsumo Kedaruge
- Oshi No Ko Chapter 164: Release Date, Where To Read, Expected Plot And More
- Watch Gold Raiders (2024) Movie
- Anime De Imadoki!
- Amazing Grace Song Download
- Mashiroiro Symphony: The Color Of Lovers Event
- 多聞くん今どっち!? 4 [Tamon-kun Ima Docchi!? 4]
- "Ich konnte nicht still sein": Michael J. Fox erzählt sein ganzes Leben
- Honeytrap: Is the 2014 Movie Based on Real People?
- 50 Inspirational Quotes to Uplift and Motivate
- Starbound Admin Commands List - GameserverKings
- Where Was Lecture Room Filmed At
- half round bead mixed size half
- pearls white nail beads rhinestone half round mixed size beige flatback pearls for diy manicure
- decals nail art accessories nail supplies
- decoration lapro010
- decoration foils tattoo
- lines animal
- ji621
- nails diy design silicone
- stamping plates image transfer
- design stencil
Article information
Author: Pres. Lawanda Wiegand
Last Updated:
Views: 6206
Rating: 4 / 5 (51 voted)
Reviews: 82% of readers found this page helpful
Author information
Name: Pres. Lawanda Wiegand
Birthday: 1993-01-10
Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893
Phone: +6806610432415
Job: Dynamic Manufacturing Assistant
Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting
Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.