Skip to content

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

Merged
merged 30 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7a26837
Allow step!=1 in slicing Series with IntervalIndex
jbrockmendel Feb 2, 2020
b371805
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 3, 2020
b78c4c3
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 3, 2020
e3631d9
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 3, 2020
7949ffd
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 3, 2020
4c2e703
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 4, 2020
88a7ace
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 4, 2020
0da92c5
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 4, 2020
f833f7c
Whatsnew
jbrockmendel Feb 5, 2020
041edbc
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 6, 2020
a2eb9bb
doc suggestion
jbrockmendel Feb 6, 2020
6b97b09
test for interval step
jbrockmendel Feb 6, 2020
abdbd76
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 6, 2020
a678ec2
remove commented-out
jbrockmendel Feb 6, 2020
2c684cc
reword whatsnew
jbrockmendel Feb 6, 2020
e4cc38d
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 6, 2020
10165b4
disallow label-based with step!=1
jbrockmendel Feb 6, 2020
8308eb8
black fixup
jbrockmendel Feb 6, 2020
2bffbda
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 17, 2020
0bdb7b5
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 22, 2020
adf0775
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 25, 2020
2744a4b
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Feb 25, 2020
cf1a393
update error message
jbrockmendel Feb 25, 2020
74a40ff
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 2, 2020
9a04d41
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 3, 2020
f6360a6
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 3, 2020
d636108
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 5, 2020
9222513
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 5, 2020
2a698c0
typo fixup
jbrockmendel Mar 7, 2020
d18c7a1
Merge branch 'master' of https://github.com/pandas-dev/pandas into co…
jbrockmendel Mar 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^

- :class:`Styler` may now render CSS more efficiently where multiple cells have the same styling (:issue:`30876`)
- Bug in :class:`IntervalIndex` slicing that prevented slicing with ``step > 1`` (:issue:`31658`)
-
-

Expand Down Expand Up @@ -145,7 +146,6 @@ Strings

Interval
^^^^^^^^

-
-

Expand Down
5 changes: 0 additions & 5 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,6 @@ def get_indexer_for(self, target: AnyArrayLike, **kwargs) -> np.ndarray:
return self.get_indexer_non_unique(target)[0]
return self.get_indexer(target, **kwargs)

def _convert_slice_indexer(self, key: slice, kind=None):
if not (key.step is None or key.step == 1):
raise ValueError("cannot support not-default step in a slice")
return super()._convert_slice_indexer(key, kind)

@Appender(Index.where.__doc__)
def where(self, cond, other=None):
if other is None:
Expand Down
25 changes: 22 additions & 3 deletions pandas/tests/indexing/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def test_loc_with_slices(self):
with pytest.raises(NotImplementedError, match=msg):
s[Interval(3, 4, closed="left") :]

# FIXME: dont leave commented-out
# TODO with non-existing intervals ?
# s.loc[Interval(-1, 0):Interval(2, 3)]

Expand All @@ -143,9 +144,27 @@ def test_loc_with_slices(self):
tm.assert_series_equal(expected, s[:2.5])
tm.assert_series_equal(expected, s[0.1:2.5])

# slice of scalar with step != 1
with pytest.raises(ValueError):
s[0:4:2]
def test_slice_step_ne1(self):
# GH#31658 slice of scalar with step != 1
s = self.s
expected = s.iloc[0:4:2]

result = s[0:4:2]
tm.assert_series_equal(result, expected)

result2 = s[0:4][::2]
tm.assert_series_equal(result2, expected)

def test_slice_interval_step(self):
# GH#31658 allows for integer step!=1, not Interval step
s = self.s
msg = (
"cannot do slice indexing on "
"<class 'pandas.core.indexes.interval.IntervalIndex'> with "
"these indexers .* of <class 'pandas._libs.interval.Interval'>"
Copy link
Member

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

)
with pytest.raises(TypeError, match=msg):
s[0 : 4 : Interval(0, 1)]
Copy link
Member

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 ?

Copy link
Member Author

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

Copy link
Member

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.


def test_loc_with_overlap(self):

Expand Down