I have two values and know their index in an array full of nans. I want to interpolate / extrapolate all the nan's
import numpy as np
y = np.array([np.nan, np.nan, 0.75, np.nan, np.nan, np.nan, np.nan, np.nan, 2.25])
With the help of this answer I write the following:
nans, x = np.isnan(y), lambda z: z.nonzero()[0]
y[nans] = np.interp(x(nans), x(~nans), y[~nans])
My output looks as follows:
[0.75 0.75 0.75 1. 1.25 1.5 1.75 2. 2.25]
However, I would like it to be:
[0.25 0.5 0.75 1. 1.25 1.5 1.75 2. 2.25]
The increment is always a constant.
When I read the documentation of np.iterp
I see that I can specify the input-parameters left
and right
. If I don't specify left
, the value to return for x < xp[0] is fp[0].
How can I specify left
and right
in order to get the desired output?