From 233797dbfdc287e7cebbd40e10dbea673ae5a4ac Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 19 Jan 2022 20:37:23 -0800 Subject: [PATCH 1/2] BUG: IntervalArray.__setitem__ with nan raise TypeError, not ValueError --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/core/arrays/interval.py | 2 +- pandas/tests/arrays/interval/test_interval.py | 2 +- pandas/tests/series/methods/test_convert_dtypes.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 1ae76984484af..fa8d2aa4c4e0c 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:`??`) - Indexing diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 747d044e36559..cd8f0f4351630 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1140,7 +1140,7 @@ 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") + 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..6edaeecd7f908 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -114,7 +114,7 @@ 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): + 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 From 9d05e3a4c11708e55dbb958bfd8e5bf240880d0b Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 19 Jan 2022 20:39:30 -0800 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/core/arrays/interval.py | 2 ++ pandas/tests/arrays/interval/test_interval.py | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index fa8d2aa4c4e0c..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:`??`) +- 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 cd8f0f4351630..9ceded00bf159 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1140,6 +1140,8 @@ def _validate_setitem_value(self, value): return self._validate_listlike(value) if needs_float_conversion: + # 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 diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 6edaeecd7f908..57aae96f38ac7 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -114,6 +114,8 @@ 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" + # 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