Skip to content

Commit 764c460

Browse files
authored
BUG: Series[slc]=foo raising with IntervalIndex (#55258)
1 parent 2e3cb76 commit 764c460

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Interval
292292
- 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`)
293293
- Bug in :meth:`IntervalIndex.get_indexer` with datetime or timedelta intervals incorrectly matching on integer targets (:issue:`47772`)
294294
- Bug in :meth:`IntervalIndex.get_indexer` with timezone-aware datetime intervals incorrectly matching on a sequence of timezone-naive targets (:issue:`47772`)
295+
- Bug in setting values on a :class:`Series` with an :class:`IntervalIndex` using a slice incorrectly raising (:issue:`54722`)
295296
-
296297

297298
Indexing

pandas/core/indexes/base.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -4204,15 +4204,9 @@ def _convert_slice_indexer(self, key: slice, kind: Literal["loc", "getitem"]):
42044204
self._validate_indexer("slice", key.step, "getitem")
42054205
return key
42064206

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

42174211
# if we are mixed and have integers
42184212
if is_positional:

pandas/tests/indexing/interval/test_interval.py

+27
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ def test_getitem_interval_with_nans(self, frame_or_series, indexer_sl):
134134

135135
tm.assert_equal(result, expected)
136136

137+
def test_setitem_interval_with_slice(self):
138+
# GH#54722
139+
ii = IntervalIndex.from_breaks(range(4, 15))
140+
ser = Series(range(10), index=ii)
141+
142+
orig = ser.copy()
143+
144+
# This should be a no-op (used to raise)
145+
ser.loc[1:3] = 20
146+
tm.assert_series_equal(ser, orig)
147+
148+
ser.loc[6:8] = 19
149+
orig.iloc[1:4] = 19
150+
tm.assert_series_equal(ser, orig)
151+
152+
ser2 = Series(range(5), index=ii[::2])
153+
orig2 = ser2.copy()
154+
155+
# this used to raise
156+
ser2.loc[6:8] = 22 # <- raises on main, sets on branch
157+
orig2.iloc[1] = 22
158+
tm.assert_series_equal(ser2, orig2)
159+
160+
ser2.loc[5:7] = 21
161+
orig2.iloc[:2] = 21
162+
tm.assert_series_equal(ser2, orig2)
163+
137164

138165
class TestIntervalIndexInsideMultiIndex:
139166
def test_mi_intervalindex_slicing_with_scalar(self):

0 commit comments

Comments
 (0)