Skip to content

Commit

Permalink
Backport PR pandas-dev#57265: COMPAT: Numpy 2.0 casting compat
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored and meeseeksmachine committed Feb 5, 2024
1 parent e54e0e2 commit 21e6f9d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
arr = np.asarray(arr)

if np.issubdtype(arr.dtype, str):
# TODO(numpy-2.0 min): This case will raise an OverflowError above
if (casted.astype(str) == arr).all():
return casted
raise ValueError(f"string values cannot be losslessly cast to {dtype}")
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,14 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
casted = casted[0, ...]
values[indexer] = casted
try:
values[indexer] = casted
except (TypeError, ValueError) as err:
if is_list_like(casted):
raise ValueError(
"setting an array element with a sequence."
) from err
raise
return self

def putmask(
Expand Down
16 changes: 2 additions & 14 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,13 +1235,7 @@ def test_loc_setitem_empty_append_raises(self):
with pytest.raises(KeyError, match=msg):
df.loc[[0, 1], "x"] = data

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

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

msg = "|".join(
[
r"shape mismatch: value array of shape \(2,2\)",
r"cannot reshape array of size 4 into shape \(2,\)",
]
)
msg = "setting an array element with a sequence."
with pytest.raises(ValueError, match=msg):
ser.loc[range(2)] = data

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

Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,9 +1958,15 @@ def test_constructor_int64_dtype(self, any_int_dtype):

def test_constructor_raise_on_lossy_conversion_of_strings(self):
# GH#44923
with pytest.raises(
ValueError, match="string values cannot be losslessly cast to int8"
):
if not np_version_gt2:
raises = pytest.raises(
ValueError, match="string values cannot be losslessly cast to int8"
)
else:
raises = pytest.raises(
OverflowError, match="The elements provided in the data"
)
with raises:
Series(["128"], dtype="int8")

def test_constructor_dtype_timedelta_alternative_construct(self):
Expand Down

0 comments on commit 21e6f9d

Please sign in to comment.