[SciPy-user] NaN's in numpy (and Scipy)
Alan Jackson
alan at ajackson.org
Sun Jan 7 21:08:08 EST 2007
Thanks! That looks like it could be what I need.
On Sun, 07 Jan 2007 3:50:07 +0000
v-nijs at kellogg.northwestern.edu wrote:
> Alan,
>
> There is an ediff1d() function in numpy that you could use.
>
> >>> b = numpy.ediff1d(a,to_end=a[-1]-a[-2])
>
> Or if you want to specify nan's at the beginning (or end):
>
> >>> b = numpy.ediff1d(a,to_begin=numpy.nan)
>
> Not sure what that will do with your masked-arrays however.
>
> I need this function in my own time-series program as well so I was happy to find this function myself.
>
> Vincent
>
>
> ==============Original message text===============
> On Sun, 07 Jan 2007 12:56:56 am +0000 Pierre GM wrote:
>
> On Saturday 06 January 2007 19:22, Alan Jackson wrote:
> > Well for one it seems a shame to carry around the extra storage when 99.9%
> > of my data is not missing.
>
> Booleans aren't that large.
>
> > With normal arrays this could be done with
> > b = concatenate((a[1:]-a[:-1],[a[-1]-a[-2]] )) or something close to that,
> > I'm writing from memory. With masked arrays, you seem to lose the
> > 'maskedness' when they are only one-d, so the concatenate fails and you
> > have to fiddle about a bit to get it to work right. In my mind, it should
> > "just work", so that the code looks the same in either case, but I couldn't
> > get it to do that.
>
> Are you sure you use the concatenate function from numpy.core.ma ?
> And not the one from numpy ?
>
> Because if you do use the one from numpy, you get a warning that the data is
> masked in one or more location, and you end up with a basic ndarray, True. In
> the new implementation, you raise an exception (not an helpful one, but I
> can't control that)
>
> >>>import numpy as N
> >>>import numpy.core.ma as ma
> >>>x = ma.array([1,2,3], mask=[0,0,1])
> >>>print ma.concatenate([x,x])
> [1 2 -- 1 2 --]
> >>>N.concatenate([x,x])
> [1 2 3 1 2 3]
>
> Now, with the new implementation
> >>>import maskedarray as MA\
> >>>x=MA.array([1,2,3], mask=[0,0,1])
> >>>print MA.concatenate([x,x])
> [1 2 -- 1 2 --]
> >>>N.concatenate([x,x])
> AttributeError: 'NoneType' object has no attribute 'shape'
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
> ===========End of original message text===========
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
--
-----------------------------------------------------------------------
| Alan K. Jackson | To see a World in a Grain of Sand |
| alan at ajackson.org | And a Heaven in a Wild Flower, |
| www.ajackson.org | Hold Infinity in the palm of your hand |
| Houston, Texas | And Eternity in an hour. - Blake |
-----------------------------------------------------------------------
More information about the SciPy-User
mailing list