Skip to content

Commit

Permalink
BUG: Series[slc]=foo raising with IntervalIndex (#55258)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Sep 25, 2023
1 parent 2e3cb76 commit 764c460
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Interval
- Bug in :class:`Interval` ``__repr__`` not displaying UTC offsets for :class:`Timestamp` bounds. Additionally the hour, minute and second components will now be shown. (:issue:`55015`)
- Bug in :meth:`IntervalIndex.get_indexer` with datetime or timedelta intervals incorrectly matching on integer targets (:issue:`47772`)
- Bug in :meth:`IntervalIndex.get_indexer` with timezone-aware datetime intervals incorrectly matching on a sequence of timezone-naive targets (:issue:`47772`)
- Bug in setting values on a :class:`Series` with an :class:`IntervalIndex` using a slice incorrectly raising (:issue:`54722`)
-

Indexing
Expand Down
12 changes: 3 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4204,15 +4204,9 @@ def _convert_slice_indexer(self, key: slice, kind: Literal["loc", "getitem"]):
self._validate_indexer("slice", key.step, "getitem")
return key

# convert the slice to an indexer here

# special case for interval_dtype bc we do not do partial-indexing
# on integer Intervals when slicing
# TODO: write this in terms of e.g. should_partial_index?
ints_are_positional = self._should_fallback_to_positional or isinstance(
self.dtype, IntervalDtype
)
is_positional = is_index_slice and ints_are_positional
# convert the slice to an indexer here; checking that the user didn't
# pass a positional slice to loc
is_positional = is_index_slice and self._should_fallback_to_positional

# if we are mixed and have integers
if is_positional:
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ def test_getitem_interval_with_nans(self, frame_or_series, indexer_sl):

tm.assert_equal(result, expected)

def test_setitem_interval_with_slice(self):
# GH#54722
ii = IntervalIndex.from_breaks(range(4, 15))
ser = Series(range(10), index=ii)

orig = ser.copy()

# This should be a no-op (used to raise)
ser.loc[1:3] = 20
tm.assert_series_equal(ser, orig)

ser.loc[6:8] = 19
orig.iloc[1:4] = 19
tm.assert_series_equal(ser, orig)

ser2 = Series(range(5), index=ii[::2])
orig2 = ser2.copy()

# this used to raise
ser2.loc[6:8] = 22 # <- raises on main, sets on branch
orig2.iloc[1] = 22
tm.assert_series_equal(ser2, orig2)

ser2.loc[5:7] = 21
orig2.iloc[:2] = 21
tm.assert_series_equal(ser2, orig2)


class TestIntervalIndexInsideMultiIndex:
def test_mi_intervalindex_slicing_with_scalar(self):
Expand Down

0 comments on commit 764c460

Please sign in to comment.