Skip to content

Commit 8baee5d

Browse files
authored
COMPAT: Numpy 2.0 casting compat (#57265)
1 parent 77b4824 commit 8baee5d

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

Diff for: pandas/core/dtypes/cast.py

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

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

Diff for: pandas/core/internals/blocks.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,14 @@ def setitem(self, indexer, value) -> Block:
13881388
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
13891389
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
13901390
casted = casted[0, ...]
1391-
values[indexer] = casted
1391+
try:
1392+
values[indexer] = casted
1393+
except (TypeError, ValueError) as err:
1394+
if is_list_like(casted):
1395+
raise ValueError(
1396+
"setting an array element with a sequence."
1397+
) from err
1398+
raise
13921399
return self
13931400

13941401
def putmask(self, mask, new) -> list[Block]:

Diff for: pandas/tests/indexing/test_loc.py

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

1227-
msg = "|".join(
1228-
[
1229-
"cannot copy sequence with size 2 to array axis with dimension 0",
1230-
r"could not broadcast input array from shape \(2,\) into shape \(0,\)",
1231-
"Must have equal len keys and value when setting with an iterable",
1232-
]
1233-
)
1227+
msg = "setting an array element with a sequence."
12341228
with pytest.raises(ValueError, match=msg):
12351229
df.loc[0:2, "x"] = data
12361230

@@ -1556,16 +1550,10 @@ def test_loc_setitem_2d_to_1d_raises(self):
15561550
# float64 dtype to avoid upcast when trying to set float data
15571551
ser = Series(range(2), dtype="float64")
15581552

1559-
msg = "|".join(
1560-
[
1561-
r"shape mismatch: value array of shape \(2,2\)",
1562-
r"cannot reshape array of size 4 into shape \(2,\)",
1563-
]
1564-
)
1553+
msg = "setting an array element with a sequence."
15651554
with pytest.raises(ValueError, match=msg):
15661555
ser.loc[range(2)] = data
15671556

1568-
msg = r"could not broadcast input array from shape \(2,2\) into shape \(2,?\)"
15691557
with pytest.raises(ValueError, match=msg):
15701558
ser.loc[:] = data
15711559

Diff for: pandas/tests/series/test_constructors.py

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

19501950
def test_constructor_raise_on_lossy_conversion_of_strings(self):
19511951
# GH#44923
1952-
with pytest.raises(
1953-
ValueError, match="string values cannot be losslessly cast to int8"
1954-
):
1952+
if not np_version_gt2:
1953+
raises = pytest.raises(
1954+
ValueError, match="string values cannot be losslessly cast to int8"
1955+
)
1956+
else:
1957+
raises = pytest.raises(
1958+
OverflowError, match="The elements provided in the data"
1959+
)
1960+
with raises:
19551961
Series(["128"], dtype="int8")
19561962

19571963
def test_constructor_dtype_timedelta_alternative_construct(self):

0 commit comments

Comments
 (0)