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/indexing.rst
+16-4
Original file line number
Diff line number
Diff line change
@@ -78,8 +78,9 @@ of multi-axis indexing.
78
78
*label* of the index. This use is **not** an integer position along the
79
79
index)
80
80
- A list or array of labels ``['a', 'b', 'c']``
81
-
- A slice object with labels ``'a':'f'``, (note that contrary to usual python
82
-
slices, **both** the start and the stop are included!)
81
+
- A slice object with labels ``'a':'f'`` (note that contrary to usual python
82
+
slices, **both** the start and the stop are included, when present in the
83
+
index!)
83
84
- A boolean array
84
85
- A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and
85
86
that returns valid output for indexing (one of the above)
@@ -330,13 +331,15 @@ Selection By Label
330
331
dfl.loc['20130102':'20130104']
331
332
332
333
pandas provides a suite of methods in order to have **purely label based indexing**. This is a strict inclusion based protocol.
333
-
**At least 1** of the labels for which you ask, must be in the index or a ``KeyError`` will be raised! When slicing, the start bound is *included*, **AND** the stop bound is *included*. Integers are valid labels, but they refer to the label **and not the position**.
334
+
**At least 1** of the labels for which you ask, must be in the index or a ``KeyError`` will be raised! When slicing, both the start bound **AND** the stop bound are *included*, if present in the index. Integers are valid labels, but they refer to the label **and not the position**.
334
335
335
336
The ``.loc`` attribute is the primary access method. The following are valid inputs:
336
337
337
338
- A single label, e.g. ``5`` or ``'a'``, (note that ``5`` is interpreted as a *label* of the index. This use is **not** an integer position along the index)
338
339
- A list or array of labels ``['a', 'b', 'c']``
339
-
- A slice object with labels ``'a':'f'`` (note that contrary to usual python slices, **both** the start and the stop are included!)
340
+
- A slice object with labels ``'a':'f'`` (note that contrary to usual python
341
+
slices, **both** the start and the stop are included, when present in the
342
+
index!)
340
343
- A boolean array
341
344
- A ``callable``, see :ref:`Selection By Callable <indexing.callable>`
342
345
@@ -370,6 +373,15 @@ Accessing via label slices
370
373
371
374
df1.loc['d':, 'A':'C']
372
375
376
+
.. warning::
377
+
378
+
If both the start and the stop labels are present in the index, then
379
+
elements *located* between the two (including them) are returned. If at
380
+
least one of the two is absent but the index is sorted (and can be compared
381
+
against start and stop labels), then slicing will still work, by selecting
382
+
labels which *rank* between the two. However, if at least one of the two is
383
+
absent *and* the index is not sorted, a ``TypeError`` will be raised.
384
+
373
385
For getting a cross section using a label (equiv to ``df.xs('a')``)
0 commit comments