Skip to content

Commit 590caa4

Browse files
BUG: IntervalArray.shift with invalid NA fill_value (#51258)
* BUG: IntervalArray.shift with invalid NA fill_value * Update doc/source/whatsnew/v2.0.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 076e1d9 commit 590caa4

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ Interval
11981198
^^^^^^^^
11991199
- Bug in :meth:`IntervalIndex.is_overlapping` incorrect output if interval has duplicate left boundaries (:issue:`49581`)
12001200
- Bug in :meth:`Series.infer_objects` failing to infer :class:`IntervalDtype` for an object series of :class:`Interval` objects (:issue:`50090`)
1201+
- Bug in :meth:`Series.shift` with :class:`IntervalDtype` and invalid null ``fill_value`` failing to raise ``TypeError`` (:issue:`51258`)
12011202
-
12021203

12031204
Indexing

pandas/core/arrays/interval.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,7 @@ def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray:
10511051
if not len(self) or periods == 0:
10521052
return self.copy()
10531053

1054-
if isna(fill_value):
1055-
fill_value = self.dtype.na_value
1054+
self._validate_scalar(fill_value)
10561055

10571056
# ExtensionArray.shift doesn't work for two reasons
10581057
# 1. IntervalArray.dtype.na_value may not be correct for the dtype.

pandas/tests/arrays/interval/test_interval.py

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def test_shift(self):
9595
expected = IntervalArray.from_tuples([(np.nan, np.nan), (1.0, 2.0)])
9696
tm.assert_interval_array_equal(result, expected)
9797

98+
msg = "can only insert Interval objects and NA into an IntervalArray"
99+
with pytest.raises(TypeError, match=msg):
100+
a.shift(1, fill_value=pd.NaT)
101+
98102
def test_shift_datetime(self):
99103
# GH#31502, GH#31504
100104
a = IntervalArray.from_breaks(date_range("2000", periods=4))
@@ -106,6 +110,10 @@ def test_shift_datetime(self):
106110
expected = a.take([1, 2, -1], allow_fill=True)
107111
tm.assert_interval_array_equal(result, expected)
108112

113+
msg = "can only insert Interval objects and NA into an IntervalArray"
114+
with pytest.raises(TypeError, match=msg):
115+
a.shift(1, fill_value=np.timedelta64("NaT", "ns"))
116+
109117

110118
class TestSetitem:
111119
def test_set_na(self, left_right_dtypes):

0 commit comments

Comments
 (0)