Skip to content
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

BUG: _validate_setitem_value fails to raise for PandasArray #51044 #54575

17 changes: 17 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,23 @@ def to_numpy(

return result

def _validate_setitem_value(self, value):
kind = self.dtype.kind
if kind == "b":
if lib.is_bool(value):
return value

elif kind == "f":
if lib.is_integer(value) or lib.is_float(value):
return value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these checks are correct for scalar value, not sure if validate_setitem_value needs to handle listlike?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeError still occurs if listlike values are entered in to validate causing tests to fail, even if it's supposed to work, see pandas/tests/arrays/numpy_/test_numpy.py test_ufunc_unary

else:
if lib.is_integer(value) or (lib.is_float(value) and value.is_integer()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only for integer-dtype. also you'll need to check itemsize to be sure it fits. there is probably something in core.dtypes.cast (maybe np_can_hold_element?) that can be reused here

return value
if NumpyEADtype(type(value)) != self.dtype:
raise TypeError("bad")
else:
raise TypeError(f"Invalid value '{str(value)}' for dtype {self.dtype}")

# ------------------------------------------------------------------------
# Ops

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,11 @@ def test_array_to_numpy_na():
result = arr.to_numpy(na_value=True, dtype=bool)
expected = np.array([True, True])
tm.assert_numpy_array_equal(result, expected)


def test_array_validate_setitem_value():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can go in tests.arrays.numpy_.test_numpy.py

# Issue# 51044
arr = pd.Series(range(5)).array
with pytest.raises(TypeError, match="bad"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put pytest.raises around each of the bad calls below. otherwise when the first one raises, the second one doesnt get tested

arr._validate_setitem_value("foo")
arr._validate_setitem_value(1.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test that 1.5 doesn't raise on float/complex-dtype