If you compare np.nan
to some other value and you do it in a np.frompyfunc
, will raise a warning. Why is this? Is it a bug or a feature?
import numpy as np
func = np.frompyfunc(lambda x: x<0,nin=1,nout=1)
print(func(1)) # False, no warning
print(np.nan<0) # False
print(func(np.nan)) # False, and raises a warning
Background and Motivation
I ran into this while debugging Why does numpy.vectorize give a warning about an invalid value when using uncertainties? The root cause was hidden behind both np.vectorize
and some other library code. So understanding that float comparison changes semantics in a frompyfunc
was a bit hard. It comes across as a bug to me.
In my own code, I like to use np.seterr(all='raise')
and with np.errstate(some_group='ignore'): ...
to make sure all warnings about float errors are handeled correctly and warnings are ignored only where I know it is safe. Therefore, I would like to know why this compare-with-nan is only occasionally warning.