Sounik Sadhu
Sounik Sadhu

Reputation: 35

How to replace `0`s with missing values according to a series of numbers in a numpy array?

I have a numpy array with a series of ints. I want to fill up the 0s with the missing values in the series.

Originally, I have these values in a column of a pandas dataframe, but for simplicity, I decided to post the question using a numpy array instead.

>>> a = np.array([15, 25, 0, 45, 0, 0, 75, 85])
>>> a
>>> array([15, 25,  0, 45,  0,  0, 75, 85])

I want the output as,

>>> array([15, 25,  35, 45,  55,  65, 75, 85])

I want to solve this problem without using loops as that would defeat the purpose of using numpy or pandas and the code would be much slower using loops.

I would not want to replace the entire column with new values which would as a side effect update the 0s as well.

I only want to update the 0s with the missing values according to the series.

Upvotes: 0

Views: 131

Answers (1)

yatu
yatu

Reputation: 88226

This is straight forward with pandas. You can apply a linear interpolation using interpolate (its default interpolation method is linear):

a = np.array([15, 25, 0, 45, 0, 0, 75, 85])
s = pd.Series(a)
s.mask(s.eq(0)).interpolate()

0    15.0
1    25.0
2    35.0
3    45.0
4    55.0
5    65.0
6    75.0
7    85.0
dtype: float64

Upvotes: 3

Related Questions