Skip to content

Commit fad50af

Browse files
committed
Merge pull request #3939 from jreback/partial_string
DOC: partial string indexing docs in timeseries.rst (GH3938)
2 parents 7ea6bdc + 6361687 commit fad50af

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

doc/source/timeseries.rst

+76-20
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ dates outside of those dates if specified.
175175
.. _timeseries.datetimeindex:
176176

177177
DatetimeIndex
178-
~~~~~~~~~~~~~
178+
-------------
179179

180180
One of the main uses for ``DatetimeIndex`` is as an index for pandas objects.
181181
The ``DatetimeIndex`` class contains many timeseries related optimizations:
@@ -189,6 +189,19 @@ The ``DatetimeIndex`` class contains many timeseries related optimizations:
189189
- Quick access to date fields via properties such as ``year``, ``month``, etc.
190190
- Regularization functions like ``snap`` and very fast ``asof`` logic
191191

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+
192205
``DatetimeIndex`` can be used like a regular index and offers all of its
193206
intelligent functionality like selection, slicing, etc.
194207

@@ -200,7 +213,10 @@ intelligent functionality like selection, slicing, etc.
200213
ts[:5].index
201214
ts[::2].index
202215
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:
204220

205221
.. ipython:: python
206222
@@ -210,12 +226,6 @@ You can pass in dates and strings that parses to dates as indexing parameters:
210226
211227
ts['10/31/2011':'12/31/2011']
212228
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-
219229
To provide convenience for accessing longer time series, you can also pass in
220230
the year or year and month as strings:
221231

@@ -225,26 +235,72 @@ the year or year and month as strings:
225235
226236
ts['2011-6']
227237
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:
230241

231242
.. ipython:: python
232243
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']
234247
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
238249

239-
.. seealso::
240-
:ref:`Reindexing methods <basics.reindexing>`
250+
.. ipython:: python
241251
242-
.. note::
252+
dft['2013-1':'2013-2']
243253
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**
247255

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
248304
249305
.. _timeseries.offsets:
250306

0 commit comments

Comments
 (0)