Skip to content

Commit cfd05f4

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

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

doc/source/indexing.rst

+13-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ 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! If at least one of the two is absent but the index elements are
84+
sorted (and can be compared against start and stop labels), then slicing
85+
will still work as expected, that is, selecting labels which rank between
86+
``'a'`` and ``'f'``.
8387
- A boolean array
8488
- A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and
8589
that returns valid output for indexing (one of the above)
@@ -330,13 +334,18 @@ Selection By Label
330334
dfl.loc['20130102':'20130104']
331335
332336
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**.
337+
**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**.
334338

335339
The ``.loc`` attribute is the primary access method. The following are valid inputs:
336340

337341
- 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)
338342
- 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!)
343+
- A slice object with labels ``'a':'f'``: note that contrary to usual python
344+
slices, **both** the start and the stop are included, when present in the
345+
index! If at least one of the two is absent but the index elements are sorted
346+
(and can be compared against start and stop labels), then slicing will still
347+
work as expected, that is selecting labels which rank between ``'a'`` and
348+
``'f'``.
340349
- A boolean array
341350
- A ``callable``, see :ref:`Selection By Callable <indexing.callable>`
342351

0 commit comments

Comments
 (0)