You can use scipy.interpolate.interp1d :
>>> from scipy.interpolate import interp1d >>> import numpy as np >>> x = np.array([10., 20., np.nan, 40., 50., np.nan, 30.]) >>> not_nan = np.logical_not(np.isnan(x)) >>> indices = np.arange(len(x)) >>> interp = interp1d(indices[not_nan], x[not_nan]) >>> interp(indices) array([ 10., 20., 30., 40., 50., 40., 30.])
EDIT : It took me a while to figure out how np.interp works, but it can do this too:
>>> np.interp(indices, indices[not_nan], x[not_nan]) array([ 10., 20., 30., 40., 50., 40., 30.])
Fred foo
source share