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
+37-4
Original file line number
Diff line number
Diff line change
@@ -78,8 +78,10 @@ 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! - also see :ref:`Slicing with labels
84
+
<indexing.slicing_with_labels>`)
83
85
- A boolean array
84
86
- A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and
85
87
that returns valid output for indexing (one of the above)
@@ -330,13 +332,16 @@ Selection By Label
330
332
dfl.loc['20130102':'20130104']
331
333
332
334
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**.
334
336
335
337
The ``.loc`` attribute is the primary access method. The following are valid inputs:
336
338
337
339
- 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
340
- 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>`)
340
345
- A boolean array
341
346
- A ``callable``, see :ref:`Selection By Callable <indexing.callable>`
342
347
@@ -390,6 +395,34 @@ For getting a value explicitly (equiv to deprecated ``df.get_value('a','A')``)
390
395
# this is also equivalent to ``df1.at['a','A']``
391
396
df1.loc['a', 'A']
392
397
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``.
0 commit comments