-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
DOC: clarify loc slice step behavior with example closes #63311 #63346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
DOC: clarify loc slice step behavior with example closes #63311 #63346
Conversation
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
doc/source/user_guide/indexing.rst
Outdated
| .. 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 |
There was a problem hiding this comment.
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?
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
doc/source/user_guide/indexing.rst
Outdated
| 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 |
There was a problem hiding this comment.
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.
| 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 |
doc/source/user_guide/indexing.rst
Outdated
| 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. |
There was a problem hiding this comment.
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.
| 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. |
|
Updated per feedback:
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 :) |
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
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
stepwith slice for label-based indexing #63311doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.AGENTS.md.