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

Joshua Ford jqford at hotmail.com
Mon Sep 29 22:31:47 EDT 2008


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...









-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080929/dff89750/attachment.html>


More information about the NumPy-Discussion mailing list