Skip to content

Commit f7a41b4

Browse files
authored
BUG: IntervalArray.__setitem__ with nan raise TypeError, not ValueError (#45484)
1 parent b0e3b33 commit f7a41b4

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Strings
205205

206206
Interval
207207
^^^^^^^^
208-
-
208+
- Bug in :meth:`IntervalArray.__setitem__` when setting ``np.nan`` into an integer-backed array raising ``ValueError`` instead of ``TypeError`` (:issue:`45484`)
209209
-
210210

211211
Indexing

pandas/core/arrays/interval.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,9 @@ def _validate_setitem_value(self, value):
11401140
return self._validate_listlike(value)
11411141

11421142
if needs_float_conversion:
1143-
raise ValueError("Cannot set float NaN to integer-backed IntervalArray")
1143+
# GH#45484 TypeError, not ValueError, matches what we get with
1144+
# non-NA un-holdable value.
1145+
raise TypeError("Cannot set float NaN to integer-backed IntervalArray")
11441146
return value_left, value_right
11451147

11461148
def value_counts(self, dropna: bool = True):

pandas/tests/arrays/interval/test_interval.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ def test_set_na(self, left_right_dtypes):
114114
result[0] = pd.NaT
115115
if result.dtype.subtype.kind in ["i", "u"]:
116116
msg = "Cannot set float NaN to integer-backed IntervalArray"
117-
with pytest.raises(ValueError, match=msg):
117+
# GH#45484 TypeError, not ValueError, matches what we get with
118+
# non-NA un-holdable value.
119+
with pytest.raises(TypeError, match=msg):
118120
result[0] = np.NaN
119121
return
120122

pandas/tests/series/methods/test_convert_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_convert_dtypes(
205205
copy = series.copy(deep=True)
206206
if is_interval_dtype(result.dtype) and result.dtype.subtype.kind in ["i", "u"]:
207207
msg = "Cannot set float NaN to integer-backed IntervalArray"
208-
with pytest.raises(ValueError, match=msg):
208+
with pytest.raises(TypeError, match=msg):
209209
result[result.notna()] = np.nan
210210
else:
211211
result[result.notna()] = np.nan

0 commit comments

Comments
 (0)