[Numpy-discussion] Filling gaps

Keith Goodman kwgoodman@gmail....
Thu Feb 12 19:52:56 CST 2009


On Thu, Feb 12, 2009 at 5:22 PM, A B <python6009@gmail.com> wrote:
> Are there any routines to fill in the gaps in an array. The simplest
> would be by carrying the last known observation forward.
> 0,0,10,8,0,0,7,0
> 0,0,10,8,8,8,7,7

Here's an obvious hack for 1d arrays:

def fill_forward(x, miss=0):
    y = x.copy()
    for i in range(x.shape[0]):
        if y[i] == miss:
            y[i] = y[i-1]
    return y

Seems to work:

>> x
   array([ 0,  0, 10,  8,  0,  0,  7,  0])
>> fill_forward(x)
   array([ 0,  0, 10,  8,  8,  8,  7,  7])


More information about the Numpy-discussion mailing list