Re: [Numpy-discussion] min() of array containing NaN

Christopher Barker wrote:
well, it's not a bug because the result if there is a NaN is undefined. However, it sure could trip people up. If you know there is likely to be a NaN in there, then you could use nanmin() or masked arrays. The problem comes up when you have no idea there might be a NaN in there, in which case you get a bogus answer -- this is very bad.
This is exactly what happened to me. I was getting crazy results when contour plotting with matplotlib, although the pcolor plots looked fine. In particular, the colorscale had incorrect limits. This led me to check the min() and max() values in my array, which were clearly wrong as illustrated by the pcolor plot. Further investigation revealed unexpected NaNs in my array.
Is there an error state that will trigger an error or warning in these situations? Otherwise, I'd have to say that the default should be to test for NaN's, and either raise an error or return NaN. If that really does slow things down too much, there could be a flag that lets you turn it off.
It is quite often the case that NaNs are unexpected, so it would be helpful to raise an Exception. Thanks for all of the helpful discussion on this issue. -- Thomas J. Duck <tom.duck@dal.ca> Associate Professor, Department of Physics and Atmospheric Science, Dalhousie University, Halifax, Nova Scotia, Canada, B3H 3J5. Tel: (902)494-1456 | Fax: (902)494-5191 | Lab: (902)494-3813 Web: http://aolab.phys.dal.ca/

On Tue, Aug 12, 2008 at 10:02 AM, Thomas J. Duck <tom.duck@dal.ca> wrote:
It is quite often the case that NaNs are unexpected, so it would be helpful to raise an Exception.
from numpy import seterr seterr(all = 'warn') Do emit a warning when encountering any kind of floating point error. You can even use raise instead of warn, in which case you will get an exception. cheers, David
participants (2)
-
David Cournapeau
-
Thomas J. Duck