diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 7d4477cd4bd8d..6c5f74f728934 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1195,6 +1195,7 @@ Interval ^^^^^^^^ - Bug in :meth:`IntervalIndex.is_overlapping` incorrect output if interval has duplicate left boundaries (:issue:`49581`) - Bug in :meth:`Series.infer_objects` failing to infer :class:`IntervalDtype` for an object series of :class:`Interval` objects (:issue:`50090`) +- Bug in :meth:`Series.shift` with :class:`IntervalDtype` and invalid null ``fill_value`` failing to raise ``TypeError`` (:issue:`51258`) - Indexing diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 338cfa18fbe66..6805d32049d34 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1051,8 +1051,7 @@ def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray: if not len(self) or periods == 0: return self.copy() - if isna(fill_value): - fill_value = self.dtype.na_value + self._validate_scalar(fill_value) # ExtensionArray.shift doesn't work for two reasons # 1. IntervalArray.dtype.na_value may not be correct for the dtype. diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index b2476a7a076fc..b97eb32a60838 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -95,6 +95,10 @@ def test_shift(self): expected = IntervalArray.from_tuples([(np.nan, np.nan), (1.0, 2.0)]) tm.assert_interval_array_equal(result, expected) + msg = "can only insert Interval objects and NA into an IntervalArray" + with pytest.raises(TypeError, match=msg): + a.shift(1, fill_value=pd.NaT) + def test_shift_datetime(self): # GH#31502, GH#31504 a = IntervalArray.from_breaks(date_range("2000", periods=4)) @@ -106,6 +110,10 @@ def test_shift_datetime(self): expected = a.take([1, 2, -1], allow_fill=True) tm.assert_interval_array_equal(result, expected) + msg = "can only insert Interval objects and NA into an IntervalArray" + with pytest.raises(TypeError, match=msg): + a.shift(1, fill_value=np.timedelta64("NaT", "ns")) + class TestSetitem: def test_set_na(self, left_right_dtypes):