[Numpy-discussion] Behavior of nan{max, min} and nanarg{max, min} for all-nan slices.

Daniele Nicolodi daniele at grinta.net
Thu Oct 3 08:34:39 EDT 2013


On 03/10/2013 13:56, Charles R Harris wrote:
> On Thu, Oct 3, 2013 at 4:06 AM, Daniele Nicolodi <daniele at grinta.net
> <mailto:daniele at grinta.net>> wrote:
> 
>     Hello,
> 
>     sorry, I don't know where exactly jump in in the thread, it is getting
>     quite long and articulated...
> 
>     On 02/10/2013 21:19, Charles R Harris wrote:
>     > The main problem I had was deciding what arg{max, min} should
>     return as
>     > the return value is an integer. I like your suggestion of returning 0.
> 
>     What about returning -1? It is still an integer (on my numpy version the
>     return value is a signed integer), it still has the property that
>     a[np.argmin(a)] == nan for a of only nans, but it is easily identifiable
>     as an anomalous return value if needed.
> 
> 
> The problem is that -1 is a valid index, whereas intp.min will always be
> out of range and lead to an IndexError if used.

If the goal is to have an error raised, just do it and do not rely on
the fact that using an invalid index will soon or later result in an error.

My proposal was a compromise to the proposal of returning 0. 0 is a
valid index that cannot be distinguished from a valid return value, -1
is a valid index but can be easily distinguished as a special case.

I don't have a strong preference between

try:
    i = np.nanargmin(a)
except ValueError:
    something()

and

i = np.nanargmin(a)
if i < 0:
    something()

but definitely I don't like returning 0:

i = np.nanargmin(a)
if i == 0:
    # uhm, wait, is the minimum at index 0 or the array is all nans?!?
    if np.isnan(a[0]):
        something()

Cheers,
Daniele




More information about the NumPy-Discussion mailing list