a = np.arange(100).reshape(10, 10)
fill_value = -99
a[2:4, 3:8] = fill_value
a[8, 8] = fill_value
a = ma.masked_array(a, a == fill_value)
print(a)
for shift in (-1, 1):
    for axis in (0, 1):
        a_shifted = np.roll(a, shift=shift, axis=axis)
idx = ~a_shifted.mask * a.mask
a[idx] = a_shifted[idx]
print(a)
Fill in missing values with nearest neighbour in Python numpy masked arrays?