You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/timeseries.rst
+38-14
Original file line number
Diff line number
Diff line change
@@ -475,55 +475,79 @@ DatetimeIndex Partial String Indexing also works on DataFrames with a ``MultiInd
475
475
dft2 = dft2.swaplevel(0, 1).sort_index()
476
476
dft2.loc[idx[:, '2013-01-05'], :]
477
477
478
+
.. _timeseries.slice_vs_exact_match:
479
+
478
480
Slice vs. exact match
479
481
^^^^^^^^^^^^^^^^^^^^^
480
482
481
483
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.
482
484
485
+
For example, let us consider ``Series`` object which index has minute resolution.
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:
509
528
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``.
511
530
512
-
.. code-block:: python
531
+
.. ipython:: python
513
532
514
-
dft_minute['2011-12-31 23:59']
515
-
# KeyError: '2011-12-31 23:59'
533
+
dft_minute.loc['2011-12-31 23:59']
516
534
517
-
To select a single row, use ``.loc``
535
+
Note also that ``DatetimeIndex`` resolution cannot be less precise than day.
518
536
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
520
545
521
-
dft_minute.loc['2011-12-31 23:59']
522
546
523
547
Datetime Indexing
524
548
~~~~~~~~~~~~~~~~~
525
549
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*.
527
551
528
552
These ``datetime`` objects are specific ``hours, minutes,`` and ``seconds`` even though they were not explicitly specified (they are ``0``).
0 commit comments