diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 1ae76984484af..c8b87519cc335 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -203,7 +203,7 @@ Strings Interval ^^^^^^^^ -- +- Bug in :meth:`IntervalArray.__setitem__` when setting ``np.nan`` into an integer-backed array raising ``ValueError`` instead of ``TypeError`` (:issue:`45484`) - Indexing diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 747d044e36559..9ceded00bf159 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1140,7 +1140,9 @@ def _validate_setitem_value(self, value): return self._validate_listlike(value) if needs_float_conversion: - raise ValueError("Cannot set float NaN to integer-backed IntervalArray") + # GH#45484 TypeError, not ValueError, matches what we get with + # non-NA un-holdable value. + raise TypeError("Cannot set float NaN to integer-backed IntervalArray") return value_left, value_right def value_counts(self, dropna: bool = True): diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 1162748370f3f..57aae96f38ac7 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -114,7 +114,9 @@ def test_set_na(self, left_right_dtypes): result[0] = pd.NaT if result.dtype.subtype.kind in ["i", "u"]: msg = "Cannot set float NaN to integer-backed IntervalArray" - with pytest.raises(ValueError, match=msg): + # GH#45484 TypeError, not ValueError, matches what we get with + # non-NA un-holdable value. + with pytest.raises(TypeError, match=msg): result[0] = np.NaN return diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index 1e88ddf3cd943..3a5fcf5c2929c 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -205,7 +205,7 @@ def test_convert_dtypes( copy = series.copy(deep=True) if is_interval_dtype(result.dtype) and result.dtype.subtype.kind in ["i", "u"]: msg = "Cannot set float NaN to integer-backed IntervalArray" - with pytest.raises(ValueError, match=msg): + with pytest.raises(TypeError, match=msg): result[result.notna()] = np.nan else: result[result.notna()] = np.nan