[Numpy-discussion] Numpy Nu-bee: "forward fill" function

Pierre GM pgmdevlist at gmail.com
Mon Sep 29 23:24:02 EDT 2008


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)




More information about the NumPy-Discussion mailing list