BUG: Fix error message for nanargmin/max of empty sequence#24021
Merged
seberg merged 1 commit intonumpy:mainfrom Jul 26, 2023
Merged
BUG: Fix error message for nanargmin/max of empty sequence#24021seberg merged 1 commit intonumpy:mainfrom
seberg merged 1 commit intonumpy:mainfrom
Conversation
For arrays with a length-zero dimension (e.g. shape (0, 50)), nanargmin and nanargmax would incorrectly raise `ValueError: All-NaN slice encountered`, despite there being no NaNs in the input array. This commit avoids proceeding to the all-NaNs check for size-zero arrays, falling through to argmin/argmax which handle empty sequences.
svank
commented
Jun 22, 2023
| a, mask = _replace_nan(a, -np.inf) | ||
| if mask is not None: | ||
| if mask is not None and mask.size: | ||
| mask = np.all(mask, axis=axis) |
Contributor
Author
There was a problem hiding this comment.
This seems to occur here, as np.all seems to default to True when there are no elements along an axis:
In [6]: np.all(np.empty((0, 5)), axis=0)
Out[6]: array([ True, True, True, True, True])
Member
|
Anyone has an opinion on this? I am wondering if it may be easier to just add "or empty slice" to the error. |
Contributor
Author
|
I know for my experience that led to this PR, the size-zero dimension was an unexpected symptom of another bug, so I probably would have been helped more by a specific "size-zero dimension" error than an "all-nan or size-zero slice" error (not that it's the job of |
Member
|
OK, we discussed it and agreed this is good. Thanks @svank. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For arrays with a length-zero dimension (e.g. shape (0, 50)), nanargmin and nanargmax would incorrectly raise
ValueError: All-NaN slice encountered, despite there being no NaNs in the input array. This commit avoids proceeding to the all-NaNs check for size-zero arrays, falling through to argmin/argmax which handle empty sequences.Before:
After: