Skip to content

Commit fcfe3f3

Browse files
committed
WIP to_datetime docs more rewriting
1 parent 0870ad3 commit fcfe3f3

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

doc/source/timeseries.rst

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,55 @@ future releases.
116116
Converting to Timestamps
117117
------------------------
118118

119-
To convert a list or Series of datetimes or strings (or a mixture, or NAs),
120-
you can use the ``to_datetime`` function:
119+
To convert a Series or list-like object of date-like objects e.g. strings,
120+
epochs, or a mixture, you can use the ``to_datetime`` function. When passed
121+
a Series, this returns a Series (with the same index), whilst a list-like
122+
is converted to a DatetimeIndex:
121123

122124
.. ipython:: python
123125
124-
pd.to_datetime(['Jul 31, 2009', '2010-01-10'])
126+
to_datetime(Series(['Jul 31, 2009', '2010-01-10', None]))
125127
126-
pd.to_datetime(['2005/11/23', '2010.12.31''])
128+
to_datetime(['2005/11/23', '2010.12.31'])
127129
128-
If you use dates start with the dayfirst (European style), you can pass
129-
the dayfirst flag:
130+
If you use dates which start with the day first (i.e. European style),
131+
you can pass the ``dayfirst`` flag:
130132

131133
.. ipython:: python
132134
133-
to_datetime(['12-13-2012'], dayfirst=True)
135+
to_datetime(['04-01-2012 10:00'], dayfirst=True)
134136
135-
*There is a known bug that this falls back to not dayfirst if there are
136-
inconsistent date formats.*
137+
to_datetime(['14-01-2012', '01-14-2012'], dayfirst=True)
138+
139+
.. warning::
140+
141+
You see in the above example that ``dayfirst`` isn't strict, so if a date
142+
can't be parsed with the day being first it will be parsed as if
143+
``dayfirst`` were False.
144+
145+
146+
Pass ``coerce=True`` to convert bad data to ``NaT`` (not a time):
147+
148+
.. ipython:: python
149+
150+
to_datetime(['2009-07-31', 'asd'])
151+
152+
to_datetime(['2009-07-31', 'asd'], coerce=True)
153+
154+
It's also possible to convert integer or float epoch times. The default unit
155+
for these is nanoseconds (since these are how Timestamps are stored). However,
156+
often epochs are stored in another ``unit`` which can be specified:
137157

138-
Pass ``coerce=True`` to convert data to ``NaT`` (not a time):
139158

140159
.. ipython:: python
141160
142-
pd.to_datetime(['2009-07-31', 'asd'], coerce=True)
161+
to_datetime([1])
162+
163+
to_datetime([1, 3.14], unit='s')
164+
165+
.. note::
166+
167+
Epoch times will be rounded to the nearest nanosecond.
143168

144169
.. _timeseries.daterange:
145170

0 commit comments

Comments
 (0)