Numpy Nu-bee: "forward fill" function
Hello all! I'm a python and numpy newbie and I'm having difficulty doing something that seems to be relatively straightforward. So I figured I would ask the experts! I would like to conduct the equivalent of a "forward fill" function on an array, that repeats the last non-null value until the next non-null value is reached. I saw that the scipy TimeSeries has a "forward_fill" function: http://pytseries.sourceforge.net/lib/interpolation.html I would like to do exactly what the TimeSeries "forward_fill" function does - but I don't have a time series. I simply want to convert an array [1,2,3,4,5,6,-999,-999,-999,-999,7,8] to [1,2,3,4,5,6,6,6,6,6,7,8] Here's what I've tried: from numpy import * a = array([1,2,3,4,5,6,-999,-999,-999,-999,7,8]) adata = a for i in adata: if i != -999: j = i elif i == -999: j = adata[i-1] print j else: print 'end' Of course .... this doesn't work. Many thanks in advance for any help or suggestions! As most of you can probably tell from my ugly code, I'm not a programmer...
On Monday 29 September 2008 22:31:47 Joshua Ford wrote:
I would like to conduct the equivalent of a "forward fill" function on an array, that repeats the last non-null value until the next non-null value is reached. I saw that the scipy TimeSeries has a "forward_fill" function:
http://pytseries.sourceforge.net/lib/interpolation.html
I would like to do exactly what the TimeSeries "forward_fill" function does - but I don't have a time series.
Which is quite OK, because you don't need a time series, a simple MaskedArray will do. For your example
import numpy.ma as ma import scikits.timeseries.lib as tl a= ma.masked_values(([1,2,3,4,5,6,-999,-999,-999,-999,7,8], -999) a masked_array(data = [1 2 3 4 5 6 -- -- -- -- 7 8], mask = [False False False False False False True True True True False False], fill_value=-999) tl.forward_fill(tl) masked_array(data = [1 2 3 4 5 6 6 6 6 6 7 8], mask = [False False False False False False False False False False False False], fill_value=-999)
Thank you so very much!
From: pgmdevlist@gmail.com To: numpy-discussion@scipy.org Date: Mon, 29 Sep 2008 23:24:02 -0400 Subject: Re: [Numpy-discussion] Numpy Nu-bee: "forward fill" function
On Monday 29 September 2008 22:31:47 Joshua Ford wrote:
I would like to conduct the equivalent of a "forward fill" function on an array, that repeats the last non-null value until the next non-null value is reached. I saw that the scipy TimeSeries has a "forward_fill" function:
http://pytseries.sourceforge.net/lib/interpolation.html
I would like to do exactly what the TimeSeries "forward_fill" function does - but I don't have a time series.
Which is quite OK, because you don't need a time series, a simple MaskedArray will do. For your example
import numpy.ma as ma import scikits.timeseries.lib as tl a= ma.masked_values(([1,2,3,4,5,6,-999,-999,-999,-999,7,8], -999) a masked_array(data = [1 2 3 4 5 6 -- -- -- -- 7 8], mask = [False False False False False False True True True True False False], fill_value=-999) tl.forward_fill(tl) masked_array(data = [1 2 3 4 5 6 6 6 6 6 7 8], mask = [False False False False False False False False False False False False], fill_value=-999)
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Joshua Ford
-
Pierre GM