@@ -175,7 +175,7 @@ dates outside of those dates if specified.
175
175
.. _timeseries.datetimeindex :
176
176
177
177
DatetimeIndex
178
- ~~~~~~~~~~~~~
178
+ -------------
179
179
180
180
One of the main uses for ``DatetimeIndex `` is as an index for pandas objects.
181
181
The ``DatetimeIndex `` class contains many timeseries related optimizations:
@@ -189,6 +189,19 @@ The ``DatetimeIndex`` class contains many timeseries related optimizations:
189
189
- Quick access to date fields via properties such as ``year ``, ``month ``, etc.
190
190
- Regularization functions like ``snap `` and very fast ``asof `` logic
191
191
192
+ DatetimeIndex objects has all the basic functionality of regular Index objects
193
+ and a smorgasbord of advanced timeseries-specific methods for easy frequency
194
+ processing.
195
+
196
+ .. seealso ::
197
+ :ref: `Reindexing methods <basics.reindexing >`
198
+
199
+ .. note ::
200
+
201
+ While pandas does not force you to have a sorted date index, some of these
202
+ methods may have unexpected or incorrect behavior if the dates are
203
+ unsorted. So please be careful.
204
+
192
205
``DatetimeIndex `` can be used like a regular index and offers all of its
193
206
intelligent functionality like selection, slicing, etc.
194
207
@@ -200,7 +213,10 @@ intelligent functionality like selection, slicing, etc.
200
213
ts[:5 ].index
201
214
ts[::2 ].index
202
215
203
- You can pass in dates and strings that parses to dates as indexing parameters:
216
+ Partial String Indexing
217
+ ~~~~~~~~~~~~~~~~~~~~~~~
218
+
219
+ You can pass in dates and strings that parse to dates as indexing parameters:
204
220
205
221
.. ipython :: python
206
222
@@ -210,12 +226,6 @@ You can pass in dates and strings that parses to dates as indexing parameters:
210
226
211
227
ts[' 10/31/2011' :' 12/31/2011' ]
212
228
213
- A ``truncate `` convenience function is provided that is equivalent to slicing:
214
-
215
- .. ipython :: python
216
-
217
- ts.truncate(before = ' 10/31/2011' , after = ' 12/31/2011' )
218
-
219
229
To provide convenience for accessing longer time series, you can also pass in
220
230
the year or year and month as strings:
221
231
@@ -225,26 +235,72 @@ the year or year and month as strings:
225
235
226
236
ts[' 2011-6' ]
227
237
228
- Even complicated fancy indexing that breaks the DatetimeIndex's frequency
229
- regularity will result in a ``DatetimeIndex `` (but frequency is lost):
238
+ This type of slicing will work on a DataFrame with a ``DateTimeIndex `` as well. Since the
239
+ partial string selection is a form of label slicing, the endpoints **will be ** included. This
240
+ would include matching times on an included date. Here's an example:
230
241
231
242
.. ipython :: python
232
243
233
- ts[[0 , 2 , 6 ]].index
244
+ dft = DataFrame(randn(100000 ,1 ),columns = [' A' ],index = date_range(' 20130101' ,periods = 100000 ,freq = ' T' ))
245
+ dft
246
+ dft[' 2013' ]
234
247
235
- DatetimeIndex objects has all the basic functionality of regular Index objects
236
- and a smorgasbord of advanced timeseries-specific methods for easy frequency
237
- processing.
248
+ This starts on the very first time in the month, and includes the last date & time for the month
238
249
239
- .. seealso ::
240
- :ref: `Reindexing methods <basics.reindexing >`
250
+ .. ipython :: python
241
251
242
- .. note ::
252
+ dft[ ' 2013-1 ' : ' 2013-2 ' ]
243
253
244
- While pandas does not force you to have a sorted date index, some of these
245
- methods may have unexpected or incorrect behavior if the dates are
246
- unsorted. So please be careful.
254
+ This specifies a stop time **that includes all of the times on the last day **
247
255
256
+ .. ipython :: python
257
+
258
+ dft[' 2013-1' :' 2013-2-28' ]
259
+
260
+ This specifies an **exact ** stop time (and is not the same as the above)
261
+
262
+ .. ipython :: python
263
+
264
+ dft[' 2013-1' :' 2013-2-28 00:00:00' ]
265
+
266
+ We are stopping on the included end-point as its part of the index
267
+
268
+ .. ipython :: python
269
+
270
+ dft[' 2013-1-15' :' 2013-1-15 12:30:00' ]
271
+
272
+ .. warning ::
273
+
274
+ The following selection will raises a ``KeyError ``; otherwise this selection methodology
275
+ would be inconsistent with other selection methods in pandas (as this is not a *slice *, nor does it
276
+ resolve to one)
277
+
278
+ .. code-block :: python
279
+
280
+ dft[' 2013-1-15 12:30:00' ]
281
+
282
+ To select a single row, use ``.loc ``
283
+
284
+ .. ipython :: python
285
+
286
+ dft.loc[' 2013-1-15 12:30:00' ]
287
+
288
+
289
+ Truncating & Fancy Indexing
290
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
+
292
+ A ``truncate `` convenience function is provided that is equivalent to slicing:
293
+
294
+ .. ipython :: python
295
+
296
+ ts.truncate(before = ' 10/31/2011' , after = ' 12/31/2011' )
297
+
298
+ Even complicated fancy indexing that breaks the DatetimeIndex's frequency
299
+ regularity will result in a ``DatetimeIndex `` (but frequency is lost):
300
+
301
+ .. ipython :: python
302
+
303
+ ts[[0 , 2 , 6 ]].index
248
304
249
305
.. _timeseries.offsets :
250
306
0 commit comments