Skip to content

Commit 8a4d720

Browse files
committed
DOC: clarify loc slice step behavior with example
When using .loc[start:stop:step], start/stop use label semantics but step is positional within that range. Added example showing difference from explicit label selection. Closes #63311
1 parent 944c527 commit 8a4d720

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doc/source/user_guide/indexing.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,24 @@ an error will be raised. For instance, in the above example, ``s.loc[2:5]`` woul
431431
For more information about duplicate labels, see
432432
:ref:`Duplicate Labels <duplicates>`.
433433

434+
Also, when using a slice with a step, such as ``.loc[start:stop:step]``, note that
435+
*start* and *stop* are interpreted as **labels**, while *step* is applied over
436+
the **positional index** within that label range. This means a stepped slice
437+
may return different labels than selecting an explicit list, even when they
438+
appear similar.
439+
440+
For example, in a ``Series`` with a non-contiguous integer index:
441+
442+
.. ipython:: python
443+
444+
s = pd.Series(range(10), index=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45])
445+
s.loc[10:50:5] # (10), then skip 3 positions → 35 only
446+
s.loc[[10, 15, 20, 25]] # explicit label selection
447+
448+
The first applies *step* across **positional locations** between the start/stop
449+
labels. The second selects each label directly.
450+
451+
434452
.. _indexing.integer:
435453

436454
Selection by position

0 commit comments

Comments
 (0)