Skip to content

Commit 71c5b23

Browse files
committed
DOC: behavior when slicing with missing bounds
closes #16917
1 parent a587d56 commit 71c5b23

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

doc/source/indexing.rst

+16-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ of multi-axis indexing.
7878
*label* of the index. This use is **not** an integer position along the
7979
index)
8080
- 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!)
8384
- A boolean array
8485
- A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and
8586
that returns valid output for indexing (one of the above)
@@ -330,13 +331,15 @@ Selection By Label
330331
dfl.loc['20130102':'20130104']
331332
332333
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**.
334335

335336
The ``.loc`` attribute is the primary access method. The following are valid inputs:
336337

337338
- 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)
338339
- 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!)
340343
- A boolean array
341344
- A ``callable``, see :ref:`Selection By Callable <indexing.callable>`
342345

@@ -370,6 +373,15 @@ Accessing via label slices
370373
371374
df1.loc['d':, 'A':'C']
372375
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+
373385
For getting a cross section using a label (equiv to ``df.xs('a')``)
374386

375387
.. ipython:: python

0 commit comments

Comments
 (0)