Skip to content

COMPAT: Numpy 2.0 casting compat #57265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,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 @@ -1388,7 +1388,14 @@ def setitem(self, indexer, value) -> 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(self, mask, new) -> list[Block]:
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 @@ -1224,13 +1224,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 @@ -1556,16 +1550,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 @@ -1949,9 +1949,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