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: Vague error message from ufunc np.any #20898

Closed
jakirkham opened this issue Jan 26, 2022 · 3 comments · Fixed by #21024
Closed

BUG: Vague error message from ufunc np.any #20898

jakirkham opened this issue Jan 26, 2022 · 3 comments · Fixed by #21024
Labels
00 - Bug triaged Issue/PR that was discussed in a triage meeting

Comments

@jakirkham
Copy link
Contributor

jakirkham commented Jan 26, 2022

Describe the issue:

Noticed with NumPy 1.22 that the error message from np.any is a bit vague. However NumPy 1.21 has a clearer error message. Interestingly the error type raised changed from TypeError to ValueError.

Reproduce the code example:

In [1]: import numpy as np

In [2]: a = np.array(["baz", "baz", "baz", "bar", "foo"], dtype="<U3")

In [3]: a.any()

Error message:

The NumPy 1.22 error message is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [3], in <module>
----> 1 a.any()

File ~/miniconda/envs/np122/lib/python3.10/site-packages/numpy/core/_methods.py:57, in _any(a, axis, dtype, out, keepdims, where)
     54 def _any(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
     55     # Parsing keyword arguments is currently fairly slow, so avoid it for now
     56     if where is True:
---> 57         return umr_any(a, axis, dtype, out, keepdims)
     58     return umr_any(a, axis, dtype, out, keepdims, where=where)

ValueError: invalid literal for int() with base 10: 'baz'

Compare this to the previous error message from NumPy 1.21, which was:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [3], in <module>
----> 1 a.any()

File ~/miniconda/envs/np121/lib/python3.10/site-packages/numpy/core/_methods.py:57, in _any(a, axis, dtype, out, keepdims, where)
     54 def _any(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
     55     # Parsing keyword arguments is currently fairly slow, so avoid it for now
     56     if where is True:
---> 57         return umr_any(a, axis, dtype, out, keepdims)
     58     return umr_any(a, axis, dtype, out, keepdims, where=where)

TypeError: cannot perform reduce with flexible type

NumPy/Python version information:

1.22.1 3.10.2 | packaged by conda-forge | (main, Jan 14 2022, 08:02:37) [Clang 11.1.0 ]
@seberg
Copy link
Member

seberg commented Jan 26, 2022

Hmmm, it now behaves like arr.astype(bool) instead of failing immediately. Which I think is better in principle, but the truthiness of NumPy strings is a giant mess still :(.

@jakirkham
Copy link
Contributor Author

At least from a user perspective, it wasn't clear to me what was going wrong whereas the original error message this was clear

@seberg seberg added the triage review Issue/PR to be discussed at the next triage meeting label Jan 26, 2022
@seberg
Copy link
Member

seberg commented Feb 9, 2022

We discussed this a bit (in the triage meeting), and settled on just rejecting strings explicitly for now, because this is a bit weird.

@seberg seberg added triaged Issue/PR that was discussed in a triage meeting and removed triage review Issue/PR to be discussed at the next triage meeting labels Feb 9, 2022
seberg added a commit to seberg/numpy that referenced this issue Feb 9, 2022
This restores pre 1.22 NumPy behaviour for strings passed into logical
ufuncs.  Logical ufuncs should be able to cast inputs to booleans
without modifying their results.
Thus they do this now (in NumPy 1.22+).  The problem is that string to
bool casts are very strange in NumPy currently.

This should be reverted/removed once string to bool casts are well
defined.  But until then, it seems more reasonable to just reject
strings.

Closes numpygh-20898
rossbar added a commit that referenced this issue Feb 9, 2022
* API: Disallow strings in logical ufuncs

This restores pre 1.22 NumPy behaviour for strings passed into logical
ufuncs.  Logical ufuncs should be able to cast inputs to booleans
without modifying their results.
Thus they do this now (in NumPy 1.22+).  The problem is that string to
bool casts are very strange in NumPy currently.

This should be reverted/removed once string to bool casts are well
defined.  But until then, it seems more reasonable to just reject
strings.

Closes gh-20898

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
charris pushed a commit to charris/numpy that referenced this issue Mar 2, 2022
* API: Disallow strings in logical ufuncs

This restores pre 1.22 NumPy behaviour for strings passed into logical
ufuncs.  Logical ufuncs should be able to cast inputs to booleans
without modifying their results.
Thus they do this now (in NumPy 1.22+).  The problem is that string to
bool casts are very strange in NumPy currently.

This should be reverted/removed once string to bool casts are well
defined.  But until then, it seems more reasonable to just reject
strings.

Closes numpygh-20898

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
melissawm pushed a commit to melissawm/numpy that referenced this issue Apr 12, 2022
* API: Disallow strings in logical ufuncs

This restores pre 1.22 NumPy behaviour for strings passed into logical
ufuncs.  Logical ufuncs should be able to cast inputs to booleans
without modifying their results.
Thus they do this now (in NumPy 1.22+).  The problem is that string to
bool casts are very strange in NumPy currently.

This should be reverted/removed once string to bool casts are well
defined.  But until then, it seems more reasonable to just reject
strings.

Closes numpygh-20898

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
seberg added a commit to seberg/numpy that referenced this issue Apr 24, 2022
* API: Disallow strings in logical ufuncs

This restores pre 1.22 NumPy behaviour for strings passed into logical
ufuncs.  Logical ufuncs should be able to cast inputs to booleans
without modifying their results.
Thus they do this now (in NumPy 1.22+).  The problem is that string to
bool casts are very strange in NumPy currently.

This should be reverted/removed once string to bool casts are well
defined.  But until then, it seems more reasonable to just reject
strings.

Closes numpygh-20898

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug triaged Issue/PR that was discussed in a triage meeting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants