Skip to content

BUG: Fix error message for nanargmin/max of empty sequence#24021

Merged
seberg merged 1 commit intonumpy:mainfrom
svank:nanargmin-empty-seq
Jul 26, 2023
Merged

BUG: Fix error message for nanargmin/max of empty sequence#24021
seberg merged 1 commit intonumpy:mainfrom
svank:nanargmin-empty-seq

Conversation

@svank
Copy link
Contributor

@svank svank commented Jun 22, 2023

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:

In [1]: import numpy as np

In [2]: x = np.zeros((0, 50))

In [3]: np.nanargmin(x)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 np.nanargmin(x)

File ~/.anaconda/envs/numpydev/lib/python3.10/site-packages/numpy/lib/nanfunctions.py:552, in nanargmin(a, axis, out, keepdims)
    550     mask = np.all(mask, axis=axis)
    551     if np.any(mask):
--> 552         raise ValueError("All-NaN slice encountered")
    553 res = np.argmin(a, axis=axis, out=out, keepdims=keepdims)
    554 return res

ValueError: All-NaN slice encountered

After:

In [1]: import numpy as np

In [2]: x = np.zeros((0, 50))

In [3]: np.nanargmin(x)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 np.nanargmin(x)

File ~/.anaconda/envs/numpydev/lib/python3.10/site-packages/numpy/lib/nanfunctions.py:553, in nanargmin(a, axis, out, keepdims)
    551     if np.any(mask):
    552         raise ValueError("All-NaN slice encountered")
--> 553 res = np.argmin(a, axis=axis, out=out, keepdims=keepdims)
    554 return res

File ~/.anaconda/envs/numpydev/lib/python3.10/site-packages/numpy/core/fromnumeric.py:1325, in argmin(a, axis, out, keepdims)
   1238 """
   1239 Returns the indices of the minimum values along an axis.
   1240 
   (...)
   1322 (2, 1, 4)
   1323 """
   1324 kwds = {'keepdims': keepdims} if keepdims is not np._NoValue else {}
-> 1325 return _wrapfunc(a, 'argmin', axis=axis, out=out, **kwds)

File ~/.anaconda/envs/numpydev/lib/python3.10/site-packages/numpy/core/fromnumeric.py:59, in _wrapfunc(obj, method, *args, **kwds)
     56     return _wrapit(obj, method, *args, **kwds)
     58 try:
---> 59     return bound(*args, **kwds)
     60 except TypeError:
     61     # A TypeError occurs if the object does have such a method in its
     62     # class, but its signature is not identical to that of NumPy's. This
   (...)
     66     # Call _wrapit from within the except clause to ensure a potential
     67     # exception has a traceback chain.
     68     return _wrapit(obj, method, *args, **kwds)

ValueError: attempt to get argmin of an empty sequence

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.
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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])

@seberg
Copy link
Member

seberg commented Jun 26, 2023

Anyone has an opinion on this? I am wondering if it may be easier to just add "or empty slice" to the error.

@InessaPawson InessaPawson added the triage review Issue/PR to be discussed at the next triage meeting label Jul 5, 2023
@svank
Copy link
Contributor Author

svank commented Jul 6, 2023

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 nanmedian to debug my code for me).

@seberg
Copy link
Member

seberg commented Jul 26, 2023

OK, we discussed it and agreed this is good. Thanks @svank.

@seberg seberg merged commit d2f63f2 into numpy:main Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug triage review Issue/PR to be discussed at the next triage meeting

Projects

Development

Successfully merging this pull request may close these issues.

3 participants