@@ -116,30 +116,55 @@ future releases.
116
116
Converting to Timestamps
117
117
------------------------
118
118
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:
121
123
122
124
.. ipython :: python
123
125
124
- pd. to_datetime([' Jul 31, 2009' , ' 2010-01-10' ] )
126
+ to_datetime(Series( [' Jul 31, 2009' , ' 2010-01-10' , None ]) )
125
127
126
- pd. to_datetime([' 2005/11/23' , ' 2010.12.31' ' ])
128
+ to_datetime([' 2005/11/23' , ' 2010.12.31' ])
127
129
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:
130
132
131
133
.. ipython :: python
132
134
133
- to_datetime([' 12-13 -2012' ], dayfirst = True )
135
+ to_datetime([' 04-01 -2012 10:00 ' ], dayfirst = True )
134
136
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:
137
157
138
- Pass `` coerce=True `` to convert data to `` NaT`` (not a time):
139
158
140
159
.. ipython :: python
141
160
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.
143
168
144
169
.. _timeseries.daterange :
145
170
0 commit comments