[Numpy-discussion] sorting -inf, nan, inf

Rick White rlw at stsci.edu
Wed Sep 20 07:09:57 CDT 2006

On Sep 19, 2006, at 9:45 PM, Tim Hochberg wrote:

> Perhaps there's some use for the sort to end behaviour that I'm  
> missing,
> but the raise an exception behaviour sure looks a lot more  
> appealing to me.

FYI, in IDL the NaN values wind up at the end of the sorted array.   
That's true despite the fact that IDL does respect all the comparison  
properties of NaNs (i.e. Value>NaN, Value<NaN, and Value==NaN are all  
false for any value).  So clearly the sort behavior was created  
deliberately.  It's also the case that the median for arrays  
including NaN values is computed as the median of the defined values,  
ignoring the NaNs.

My view is that if the user has NaN values in the array, sort should  
respect the float exception flags and should only raise an exception  
if that is what the user has requested.

> Here's a strawman proposal:
>     Sort the array. Then examine numpy.geterr()['invalid']. If it  
> is not
>     'ignore', then check examine sometrue(isnan(thearray)). If the
>     latter is true then raise and error, issue a warning or call the
>     error reporting functioni as appropriate. Note that we always sort
>     the array to be consistent with the behaviour of the ufuncs that
>     proceed even when they end up raising an exception.

Here's another proposal: Make a first pass through the array,  
replacing NaN values with Inf and counting the NaNs ("nancount").  
Raise an exception at this point if NaNs are not supposed to be  
allowed.  Otherwise sort the array, and then as the last step replace  
the trailing NaNcount values with NaN.

It seems to me that this would give predictable results while  
respecting the exception flags at little extra cost.

More information about the Numpy-discussion mailing list