Skip to content

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

Merged
merged 4 commits into from
Jun 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Data manipulations
merge
concat

Top-level Missing Data
Top-level missing data
~~~~~~~~~~~~~~~~~~~~~~

.. currentmodule:: pandas.core.common
Expand All @@ -137,6 +137,17 @@ Top-level Missing Data
isnull
notnull

Top-level dealing with datetimes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. currentmodule:: pandas.tseries.tools

.. autosummary::
:toctree: generated/

to_datetime


Standard moving window functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
62 changes: 62 additions & 0 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

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

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'])
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do u think that it should convert by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think errors='raise' should be the default...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi...changing errors='raise' in theory a good idea, but breaks bunch of stuff.....will have to decide on that later...otherwise this looks ready?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down