Skip to content

Commit 2c61370

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

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

doc/source/indexing.rst

+37-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ 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! - also see :ref:`Slicing with labels
84+
<indexing.slicing_with_labels>`)
8385
- A boolean array
8486
- A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and
8587
that returns valid output for indexing (one of the above)
@@ -330,13 +332,16 @@ Selection By Label
330332
dfl.loc['20130102':'20130104']
331333
332334
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**.
335+
**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**.
334336

335337
The ``.loc`` attribute is the primary access method. The following are valid inputs:
336338

337339
- 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)
338340
- 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!)
341+
- A slice object with labels ``'a':'f'`` (note that contrary to usual python
342+
slices, **both** the start and the stop are included, when present in the
343+
index! - also See :ref:`Slicing with labels
344+
<indexing.slicing_with_labels>`)
340345
- A boolean array
341346
- A ``callable``, see :ref:`Selection By Callable <indexing.callable>`
342347

@@ -390,6 +395,34 @@ For getting a value explicitly (equiv to deprecated ``df.get_value('a','A')``)
390395
# this is also equivalent to ``df1.at['a','A']``
391396
df1.loc['a', 'A']
392397
398+
.. _indexing.slicing_with_labels:
399+
400+
Slicing with labels
401+
~~~~~~~~~~~~~~~~~~~
402+
403+
When using ``.loc`` with slices, if both the start and the stop labels are
404+
present in the index, then elements *located* between the two (including them)
405+
are returned:
406+
407+
.. ipython:: python
408+
409+
s = pd.Series(list('abcde'), index=[0,3,2,5,4])
410+
s.loc[3:5]
411+
412+
If at least one of the two is absent, but the index is sorted, and can be
413+
compared against start and stop labels, then slicing will still work as
414+
expected, by selecting labels which *rank* between the two:
415+
416+
.. ipython:: python
417+
418+
s.sort_index()
419+
s.sort_index().loc[1:6]
420+
421+
However, if at least one of the two is absent *and* the index is not sorted, an
422+
error will be raised (since doing otherwise would be computationally expensive,
423+
as well as potentially ambiguous for mixed type indexes). For instance, in the
424+
above example, ``s.loc[1:6]`` would raise ``KeyError``.
425+
393426
.. _indexing.integer:
394427

395428
Selection By Position

0 commit comments

Comments
 (0)