Skip to content

Conversation

@PR-HARIHARAN
Copy link

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.

image

Closes #63311

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 pandas-dev#63311
.. ipython:: python
s = pd.Series(range(10), index=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45])
s.loc[10:50:5] # (10), then skip 3 positions → 35 only
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also show a similar example where the index is not numeric?

@mroeschke mroeschke added the Docs label Dec 12, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

For more information about duplicate labels, see
:ref:`Duplicate Labels <duplicates>`.

Also, when using a slice with a step, such as ``.loc[start:stop:step]``, note that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous paragraph also starts with Also,, just from a prose standpoint I think we should avoid this.

Suggested change
Also, when using a slice with a step, such as ``.loc[start:stop:step]``, note that
When using a slice with a step, such as ``.loc[start:stop:step]``, note that

Comment on lines 436 to 438
the **positional index** within that label range. This means a stepped slice
may return different labels than selecting an explicit list, even when they
appear similar.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be made a little more specific.

Suggested change
the **positional index** within that label range. This means a stepped slice
may return different labels than selecting an explicit list, even when they
appear similar.
the **positional index** within that label range. This means a stepped slice
will behave differently than using the labels `range(start, stop, step)` when
the index is not contiguous integers.

@PR-HARIHARAN
Copy link
Author

PR-HARIHARAN commented Dec 13, 2025

Updated per feedback:

  1. Removed "Also," to avoid prose repetition
  2. Added string-based index example showing the step behavior is index-type independent

The string index example clarifies that step is positional regardless of whether you use integers, characters, or other index types.

Pre-commit checks passed ✓

Happy to help, if any further updates are needed :)

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rhshadrach rhshadrach added the Indexing Related to indexing on series/frames, not to indexes themselves label Dec 13, 2025
@rhshadrach rhshadrach added this to the 3.0 milestone Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Docs Indexing Related to indexing on series/frames, not to indexes themselves

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Inconsistent behavior for step with slice for label-based indexing

3 participants