-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: allow step!=1 slice with IntervalIndex #31658
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
API: allow step!=1 slice with IntervalIndex #31658
Conversation
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
I suppose should add a whatsnew bug fix entry. |
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.
Is there a test that ensures that slicing with an interval as step still raises an error?
doc/source/whatsnew/v1.1.0.rst
Outdated
@@ -145,7 +145,7 @@ Strings | |||
|
|||
Interval | |||
^^^^^^^^ | |||
|
|||
- Bug in :class:`IntervalIndex` slicing that prevented slicing with ``step > 1`` (:issue:`31658`) |
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 would put this in the "other enhancements" section
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.
@jbrockmendel what would you expect for this:
s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(2, 14, 2)))
s[1.5:9.5:2]
I think that is less clear, which might be the reason it was disallowed initially.
"these indexers .* of <class 'pandas._libs.interval.Interval'>" | ||
) | ||
with pytest.raises(TypeError, match=msg): | ||
s[0 : 4 : Interval(0, 1)] |
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 would test it where start and stop are also intervals.
And can you move it to the test_loc_with_slices
a bit above that is testing slicing with Intervals ?
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.
start and stop are also intervals
out of scope
And can you move it to the test_loc_with_slices a bit above that is testing slicing with Intervals ?
sure
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.
start and stop are also intervals
out of scope
I think that is not out of scope, as that hits this path, and right now actually ensured there is a proper error message.
msg = ( | ||
"cannot do slice indexing on " | ||
"<class 'pandas.core.indexes.interval.IntervalIndex'> with " | ||
"these indexers .* of <class 'pandas._libs.interval.Interval'>" |
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 current error message I get is more comprehensible I think (with s
from the tests here):
In [41]: s[Interval(3, 4):Interval(4, 5):Interval(1, 2)]
...
ValueError: cannot support not-default step in a slice
In [42]: s[1:2:Interval(1, 2)]
...
ValueError: cannot support not-default step in a slice
I agree that is less obvious, as that is not positional. |
So let's not allow that? |
…nvert_scalar_indexer
OK I see, I thought that the float-slice would have been disallowed somewhere else, but evidently not. So yes, this needs to be updated to restore that. |
@jorisvandenbossche double-checking: suppose we get integer sllce that is valid for either loc or iloc:
For the slice we treat the ambiguous |
Closing to clear the queue (in particular #31786 should happen before this), will revisit |
…nvert_scalar_indexer
can you rebase. look ok to me. @jschendel |
…nvert_scalar_indexer
rebased+green |
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
can you rebase. |
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
…nvert_scalar_indexer
rebased+green |
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, thanks!
pandas/core/indexers.py
Outdated
@@ -20,6 +21,34 @@ | |||
# Indexer Identification | |||
|
|||
|
|||
def is_valid_positional_slice(slc: slice) -> bool: | |||
""" | |||
Check if a slice object can be interpretd as a positional indexer. |
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.
nitpick: interpretd --> interpreted
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.
updated
…nvert_scalar_indexer
thanks 2 followups:
|
* Allow step!=1 in slicing Series with IntervalIndex * Whatsnew * doc suggestion * test for interval step * remove commented-out * reword whatsnew * disallow label-based with step!=1 * black fixup * update error message * typo fixup
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
See discussion #16386 (comment)
cc @jorisvandenbossche @jschendel