-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Changes from 4 commits
ea10835
b112d5a
352c6ce
dc541d1
a219e89
61768d9
2d79270
45c2f98
e353d1d
09a49ea
89d2b66
c5e0e24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
else: | ||
if lib.is_integer(value) or (lib.is_float(value) and value.is_integer()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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