import numpy as np
a = np.array([2, 6, 4, 8])
mymask = a > 5
b = np.ma.masked_where(mymask, a)
print(b)
c = np.interp(np.where(mymask)[0], np.where(~mymask)[0], b[np.where(~mymask
    )[0]])
b[np.where(mymask)[0]] = c
print(b)
How to replace masked values from a numpy array with the average of the values immediately next to the masked value