Skip to content

Commit c1b17ae

Browse files
Backport PR #57265 on branch 2.2.x (COMPAT: Numpy 2.0 casting compat) (#57271)
Backport PR #57265: COMPAT: Numpy 2.0 casting compat Co-authored-by: Matthew Roeschke <[email protected]>
1 parent e54e0e2 commit c1b17ae

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

pandas/core/dtypes/cast.py

+1
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
16821682
arr = np.asarray(arr)
16831683

16841684
if np.issubdtype(arr.dtype, str):
1685+
# TODO(numpy-2.0 min): This case will raise an OverflowError above
16851686
if (casted.astype(str) == arr).all():
16861687
return casted
16871688
raise ValueError(f"string values cannot be losslessly cast to {dtype}")

pandas/core/internals/blocks.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,14 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
14251425
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
14261426
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
14271427
casted = casted[0, ...]
1428-
values[indexer] = casted
1428+
try:
1429+
values[indexer] = casted
1430+
except (TypeError, ValueError) as err:
1431+
if is_list_like(casted):
1432+
raise ValueError(
1433+
"setting an array element with a sequence."
1434+
) from err
1435+
raise
14291436
return self
14301437

14311438
def putmask(

pandas/tests/indexing/test_loc.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,7 @@ def test_loc_setitem_empty_append_raises(self):
12351235
with pytest.raises(KeyError, match=msg):
12361236
df.loc[[0, 1], "x"] = data
12371237

1238-
msg = "|".join(
1239-
[
1240-
"cannot copy sequence with size 2 to array axis with dimension 0",
1241-
r"could not broadcast input array from shape \(2,\) into shape \(0,\)",
1242-
"Must have equal len keys and value when setting with an iterable",
1243-
]
1244-
)
1238+
msg = "setting an array element with a sequence."
12451239
with pytest.raises(ValueError, match=msg):
12461240
df.loc[0:2, "x"] = data
12471241

@@ -1575,16 +1569,10 @@ def test_loc_setitem_2d_to_1d_raises(self):
15751569
# float64 dtype to avoid upcast when trying to set float data
15761570
ser = Series(range(2), dtype="float64")
15771571

1578-
msg = "|".join(
1579-
[
1580-
r"shape mismatch: value array of shape \(2,2\)",
1581-
r"cannot reshape array of size 4 into shape \(2,\)",
1582-
]
1583-
)
1572+
msg = "setting an array element with a sequence."
15841573
with pytest.raises(ValueError, match=msg):
15851574
ser.loc[range(2)] = data
15861575

1587-
msg = r"could not broadcast input array from shape \(2,2\) into shape \(2,?\)"
15881576
with pytest.raises(ValueError, match=msg):
15891577
ser.loc[:] = data
15901578

pandas/tests/series/test_constructors.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1958,9 +1958,15 @@ def test_constructor_int64_dtype(self, any_int_dtype):
19581958

19591959
def test_constructor_raise_on_lossy_conversion_of_strings(self):
19601960
# GH#44923
1961-
with pytest.raises(
1962-
ValueError, match="string values cannot be losslessly cast to int8"
1963-
):
1961+
if not np_version_gt2:
1962+
raises = pytest.raises(
1963+
ValueError, match="string values cannot be losslessly cast to int8"
1964+
)
1965+
else:
1966+
raises = pytest.raises(
1967+
OverflowError, match="The elements provided in the data"
1968+
)
1969+
with raises:
19641970
Series(["128"], dtype="int8")
19651971

19661972
def test_constructor_dtype_timedelta_alternative_construct(self):

0 commit comments

Comments
 (0)