Skip to content

Commit d215905

Browse files
committed
- Addressing code review: documentation clarification.
1 parent c287845 commit d215905

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

doc/source/timeseries.rst

+38-14
Original file line numberDiff line numberDiff line change
@@ -475,55 +475,79 @@ DatetimeIndex Partial String Indexing also works on DataFrames with a ``MultiInd
475475
dft2 = dft2.swaplevel(0, 1).sort_index()
476476
dft2.loc[idx[:, '2013-01-05'], :]
477477
478+
.. _timeseries.slice_vs_exact_match:
479+
478480
Slice vs. exact match
479481
^^^^^^^^^^^^^^^^^^^^^
480482

481483
The same string used as an indexing parameter can be treated either as a slice or as an exact match depending on the resolution of an index. If the string is less accurate than the index, it will be treated as a slice, otherwise as an exact match.
482484

485+
For example, let us consider ``Series`` object which index has minute resolution.
486+
483487
.. ipython:: python
484488
485489
series_minute = pd.Series([1, 2, 3],
486490
pd.DatetimeIndex(['2011-12-31 23:59:00',
487491
'2012-01-01 00:00:00',
488-
'2012-01-01 00:01:00']))
492+
'2012-01-01 00:02:00']))
489493
series_minute.index.resolution
490-
series_minute['2011-12-31 23'] # returns Series
491-
series_minute['2011-12-31 23:59'] # returns scalar
494+
495+
Timestamp string less accurate than minute gives ``Series`` object.
496+
497+
.. ipython:: python
498+
499+
series_minute['2011-12-31 23']
500+
501+
Timestamp string with minute resolution (or more accurate) gives scalar instead, i.e. it is not casted to a slice.
502+
503+
.. ipython:: python
504+
505+
series_minute['2011-12-31 23:59']
506+
series_minute['2011-12-31 23:59:00']
507+
508+
If index resolution is second, the minute-accurate timestamp gives ``Series``.
509+
510+
.. ipython:: python
492511
493512
series_second = pd.Series([1, 2, 3],
494513
pd.DatetimeIndex(['2011-12-31 23:59:59',
495514
'2012-01-01 00:00:00',
496515
'2012-01-01 00:00:01']))
497516
series_second.index.resolution
498-
series_second['2011-12-31 23:59'] # now it returns Series
517+
series_second['2011-12-31 23:59']
499518
500-
It also works for ``DataFrame``:
519+
If the timestamp string is treated as a slice, it can be used to index ``DataFrame`` with ``[]`` as well.
501520

502521
.. ipython:: python
503522
504523
dft_minute = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]},
505524
index=series_minute.index)
506525
dft_minute['2011-12-31 23']
507526
508-
.. warning::
527+
However if the string is treated as an exact match the selection in ``DataFrame``'s ``[]`` will be column-wise and not row-wise, see :ref:`Indexing Basics <indexing.basics>`. For example ``dft_minute['2011-12-31 23:59']`` will raise ``KeyError`` as ``'2012-12-31 23:59'`` has the same resolution as index and there is no column with such name:
509528

510-
If string used in ``DataFrame``'s ``[]`` indexing is treated as an exact match the selection will be column-wise and not row-wise. This is consistent with :ref:`Indexing Basics <indexing.basics>`. For example, the following code will raise ``KeyError`` as there is no column with index ``'2012-12-31 23:59'``:
529+
To select a single row, use ``.loc``.
511530

512-
.. code-block:: python
531+
.. ipython:: python
513532
514-
dft_minute['2011-12-31 23:59']
515-
# KeyError: '2011-12-31 23:59'
533+
dft_minute.loc['2011-12-31 23:59']
516534
517-
To select a single row, use ``.loc``
535+
Note also that ``DatetimeIndex`` resolution cannot be less precise than day.
518536

519-
.. ipython:: python
537+
.. ipython:: python
538+
539+
series_monthly = pd.Series([1, 2, 3],
540+
pd.DatetimeIndex(['2011-12',
541+
'2012-01',
542+
'2012-02']))
543+
series_monthly.index.resolution
544+
series_monthly['2011-12'] # returns Series
520545
521-
dft_minute.loc['2011-12-31 23:59']
522546
523547
Datetime Indexing
524548
~~~~~~~~~~~~~~~~~
525549

526-
Indexing a ``DateTimeIndex`` with a partial string depends on the "accuracy" of the period, in other words how specific the interval is in relation to the frequency of the index. In contrast, indexing with datetime objects is exact, because the objects have exact meaning. These also follow the semantics of *including both endpoints*.
550+
As discussed in previous section, indexing a ``DateTimeIndex`` with a partial string depends on the "accuracy" of the period, in other words how specific the interval is in relation to the resolution of the index. In contrast, indexing with datetime objects is exact, because the objects have exact meaning. These also follow the semantics of *including both endpoints*.
527551

528552
These ``datetime`` objects are specific ``hours, minutes,`` and ``seconds`` even though they were not explicitly specified (they are ``0``).
529553

0 commit comments

Comments
 (0)