-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Make bool(void_scalar) and void_scalar.astype(bool) consistent #9856
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
Conversation
I get slightly different results with Python 2.7 and 1.14-dev
So always True unless empty, no warnings. EDIT: Same with void array scalars. |
Also differs for Python 3. |
@@ -1203,6 +1203,37 @@ def test_count_nonzero_unaligned(self): | |||
a[:o] = False | |||
assert_equal(np.count_nonzero(a), builtins.sum(a.tolist())) | |||
|
|||
def _test_cast_from_flexible(self, 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.
Might want to document that this only tests scalars and scalar arrays.
So the rule in all these cases will be that 0D arrays and scalars containing empty byte strings, empty unicode strings, and V scalars converted to boolean yield False, and all other cases yield True. I assume this extends consistently to higher dimensional arrays. |
Turns out I mispoke about python 2, and hadn't tested
Can you elaborate on this? |
Yes. This was already true when converting with There's nothing special about 0d here, other than the fact that the LHS of the test |
Actually, the above is what the previous patch did. This patch only affects the visible behaviour of |
This seems correct to me, but will need a release note. |
I get
Yep, but there are tests waiting to be enabled for the others. |
You're right, I had a different execution path in my head. I've updated the top comment with correct information. |
Caused by void scalars decaying to 1d uint8 arrays before casting - `getitem` is dangerous for intermediate results Works towards numpy#9847
3e3ecfd
to
931758e
Compare
Release note added |
Thanks Eric. |
Previously the behaviour of
void.astype(bool)
was:itemsize == 0
-False
itemsize > 0
-True
itemsize == 0
-False
, warn about casting empty arrays to boolitemsize == 1
-True
iff the sole element is 0itemsize > 1
-ValueError: setting an array element with a sequence.
Compared to the behaviour of
bool(void)
, which is:True
iff all bytes are zeroThe problem is that
y = x.astype(bool)
is implemented asy[i] = bool(x[i].item())
This patch changes it to use
y[i] = bool(x[i])
.void.item()
returns different results on python 2 and 3, which causes the crash.IMO,
item()
is not something we should ever be using internally.Works towards #9847
This is a less objectionable subset of #9848 with no compatibility ramifications