-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC add to_datetime to api.rst #3859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,68 @@ scalar values and ``PeriodIndex`` for sequences of spans. Better support for | |
irregular intervals with arbitrary start and end points are forth-coming in | ||
future releases. | ||
|
||
|
||
.. _timeseries.converting: | ||
|
||
Converting to Timestamps | ||
------------------------ | ||
|
||
To convert a Series or list-like object of date-like objects e.g. strings, | ||
epochs, or a mixture, you can use the ``to_datetime`` function. When passed | ||
a Series, this returns a Series (with the same index), while a list-like | ||
is converted to a DatetimeIndex: | ||
|
||
.. ipython:: python | ||
|
||
to_datetime(Series(['Jul 31, 2009', '2010-01-10', None])) | ||
|
||
to_datetime(['2005/11/23', '2010.12.31']) | ||
|
||
If you use dates which start with the day first (i.e. European style), | ||
you can pass the ``dayfirst`` flag: | ||
|
||
.. ipython:: python | ||
|
||
to_datetime(['04-01-2012 10:00'], dayfirst=True) | ||
|
||
to_datetime(['14-01-2012', '01-14-2012'], dayfirst=True) | ||
|
||
.. warning:: | ||
|
||
You see in the above example that ``dayfirst`` isn't strict, so if a date | ||
can't be parsed with the day being first it will be parsed as if | ||
``dayfirst`` were False. | ||
|
||
|
||
Pass ``coerce=True`` to convert bad data to ``NaT`` (not a time): | ||
|
||
.. ipython:: python | ||
|
||
to_datetime(['2009-07-31', 'asd']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably could add something about raise here. (Also, I really dislike that this is default behaviour) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do u think that it should convert by default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi...changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just rebased, so will check I'm not saying anything stupid and then merge... |
||
|
||
to_datetime(['2009-07-31', 'asd'], coerce=True) | ||
|
||
It's also possible to convert integer or float epoch times. The default unit | ||
for these is nanoseconds (since these are how Timestamps are stored). However, | ||
often epochs are stored in another ``unit`` which can be specified: | ||
|
||
|
||
.. ipython:: python | ||
|
||
to_datetime([1]) | ||
|
||
to_datetime([1, 3.14], unit='s') | ||
|
||
.. note:: | ||
|
||
Epoch times will be rounded to the nearest nanosecond. | ||
|
||
Take care, ``to_datetime`` may not act as you expect on mixed data: | ||
|
||
.. ipython:: python | ||
|
||
pd.to_datetime([1, '1']) | ||
|
||
.. _timeseries.daterange: | ||
|
||
Generating Ranges of Timestamps | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lot of likes and objects here, but I think it'll do