Skip to content

Commit ad03e49

Browse files
authored
BUG: setting non-string value into StringArray raises ValueError instead of TypeError (#51191)
1 parent c64cc46 commit ad03e49

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ ExtensionArray
13241324
- Bug in :meth:`api.types.is_numeric_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``_is_numeric`` returned ``True`` (:issue:`50563`)
13251325
- Bug in :meth:`api.types.is_integer_dtype`, :meth:`api.types.is_unsigned_integer_dtype`, :meth:`api.types.is_signed_integer_dtype`, :meth:`api.types.is_float_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``kind`` returned the corresponding NumPy type (:issue:`50667`)
13261326
- Bug in :class:`Series` constructor unnecessarily overflowing for nullable unsigned integer dtypes (:issue:`38798`, :issue:`25880`)
1327+
- Bug in setting non-string value into ``StringArray`` raising ``ValueError`` instead of ``TypeError`` (:issue:`49632`)
13271328

13281329
Styler
13291330
^^^^^^

pandas/core/arrays/string_.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,14 @@ def __setitem__(self, key, value):
414414
if isna(value):
415415
value = libmissing.NA
416416
elif not isinstance(value, str):
417-
raise ValueError(
417+
raise TypeError(
418418
f"Cannot set non-string value '{value}' into a StringArray."
419419
)
420420
else:
421421
if not is_array_like(value):
422422
value = np.asarray(value, dtype=object)
423423
if len(value) and not lib.is_string_array(value, skipna=True):
424-
raise ValueError("Must provide strings.")
424+
raise TypeError("Must provide strings.")
425425

426426
value[isna(value)] = libmissing.NA
427427

pandas/core/arrays/string_arrow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ def _maybe_convert_setitem_value(self, value):
163163
if isna(value):
164164
value = None
165165
elif not isinstance(value, str):
166-
raise ValueError("Scalar must be NA or str")
166+
raise TypeError("Scalar must be NA or str")
167167
else:
168168
value = np.array(value, dtype=object, copy=True)
169169
value[isna(value)] = None
170170
for v in value:
171171
if not (v is None or isinstance(v, str)):
172-
raise ValueError("Scalar must be NA or str")
172+
raise TypeError("Scalar must be NA or str")
173173
return super()._maybe_convert_setitem_value(value)
174174

175175
def isin(self, values) -> npt.NDArray[np.bool_]:

pandas/tests/arrays/string_/test_string.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def test_setitem_validates(cls):
5353
msg = "Cannot set non-string value '10' into a StringArray."
5454
else:
5555
msg = "Scalar must be NA or str"
56-
with pytest.raises(ValueError, match=msg):
56+
with pytest.raises(TypeError, match=msg):
5757
arr[0] = 10
5858

5959
if cls is pd.arrays.StringArray:
6060
msg = "Must provide strings."
6161
else:
6262
msg = "Scalar must be NA or str"
63-
with pytest.raises(ValueError, match=msg):
63+
with pytest.raises(TypeError, match=msg):
6464
arr[:] = np.array([1, 2])
6565

6666

@@ -403,12 +403,10 @@ def test_fillna_args(dtype, request):
403403
tm.assert_extension_array_equal(res, expected)
404404

405405
if dtype.storage == "pyarrow":
406-
err = TypeError
407406
msg = "Invalid value '1' for dtype string"
408407
else:
409-
err = ValueError
410408
msg = "Cannot set non-string value '1' into a StringArray."
411-
with pytest.raises(err, match=msg):
409+
with pytest.raises(TypeError, match=msg):
412410
arr.fillna(value=1)
413411

414412

@@ -574,7 +572,7 @@ def test_setitem_scalar_with_mask_validation(dtype):
574572
msg = "Cannot set non-string value"
575573
else:
576574
msg = "Scalar must be NA or str"
577-
with pytest.raises(ValueError, match=msg):
575+
with pytest.raises(TypeError, match=msg):
578576
ser[mask] = 1
579577

580578

0 commit comments

Comments
 (0)