[SciPy-user] nan?

Robert Kern robert.kern at gmail.com
Thu Nov 30 03:08:24 CST 2006

Joshua Petterson wrote:
> Hi Robert,
> thanks for these precisions. I don't want to start a troll in this
> m-l, but why numpy doesn't understand nan and masked_values together?
> And a mix of them doen't work:
> |~|[40]>ma.masked_values([1,2,nan],nan).mean()
> Out [40]:array(nan)
> |~|[41]>ma.masked_object([1,2,nan],nan).mean()
> Out [41]:array(nan)

That would be the "nan != nan" feature of the little buggers at work.

> Then, if I have missing values in my datas, what can I do? I cannot
> compute on nan, and numpy.ma doesn't recognize it. I could use isnan
> to find them, but what's that?:
> |~|[48]>a=[1,2,nan]
> |~|[49]>b=ma.array(a,mask=isnan(a))
> |~|[50]>b
> Out [50]:
> array(data =
>  [  1.00000000e+00   2.00000000e+00   1.00000000e+20],
>       mask =
>  [False False True],
>       fill_value=1e+20)
> nan becomes 1e+20!!! Good precision :-)

That's the default fill value. Don't worry about it. Change it back to nan if
you like using the fill_value keyword argument. It only affects display and any
users of the .filled() method (which will probably demand some other specific
fill value depending on the application).

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the SciPy-user mailing list