[SciPy-user] NaN's in numpy (and Scipy)

Vincent Nijs v-nijs at kellogg.northwestern.edu
Sat Jan 6 16:14:00 CST 2007


It may be relevant to note that 'isnan' only seems to work with floats. If
you change Pierre's example a bit, mask creation doesn't work as I might
expect. If you create the array as follows

>>> x = N.array([1,2,N.nan,4],dtype='int16')

or

>>> x = N.arange(4)
>>> x[2] = nan

Then x = [1,2,0,4] and isnan(x) = [False,False,False,False]

It might be convenient if an array would be automatically up-cast to float
is an nan is present.

Vincent

On 1/6/07 3:25 PM, "Pierre GM" <pgmdevlist at gmail.com> wrote:

> On Saturday 06 January 2007 16:08, Alan Jackson wrote:
>> Missing data. Basically want to handle missing data as painlessly as
>> possible without having to add a bunch of stuff every time a calculation is
>> done.
> 
> Alan,
> Could you be a bit more specific about you mean by this "bunch of stuffs" ?
> What are your complaints about the current implementation of masked arrays ?
> Assuming you have Nan in your data, you can get a masked array as easily as
> that:
>>>> import numpy as N
>>>> import numpy.core.ma as MA
>>>> x = N.array([1,2,N.nan,4])
>>>> X = MA.masked_array(x, mask=N.isnan(x)
>>>> X
> array(data = 
>  [  1.00000000e+00   2.00000000e+00   1.00000000e+20   4.00000000e+00],
>       mask =
>  [False False True False],
>       fill_value=1e+20)
> 
> And then you can play with X.
> 
> A few months ago, I ran into some problems while trying to subclass
> MaskedArray. I rewrote most of numpy.core.ma to solve my particular issues.
> This new implementation is available on the scipy SVN server, in the
> sandbox/maskedarray folder. I'd be glad if you could give it a try, so that I
> could improve it.
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
> 




More information about the SciPy-user mailing list