1

I have an array of the form (simplified here): [1,NaN,NaN,7,NaN,27]. I want to replace the NaN's with values equally spaced between the known values, so the above array would become [1,3,5,7,17,27]. Is there a fast way to do this (without using some for loops)? Thank you!

2 Answers 2

2

Pandas dataframe.interpolate() function is basically used to fill NA values in the dataframe or series

import pandas as pd
import numpy as np

arr = [ 1, np.NaN, np.NaN, 7, np.NaN, 27]
//converting array in series 
print(pd.Series(arr).interpolate(method = 'linear', limit_direction = 'forward'))

Parameters
method = 'linear': Ignore the index and treat the values as equally spaced.
limit_direction : {‘forward’, ‘backward’, ‘both’}, default ‘forward’ If limit is specified, consecutive NaNs will be filled in this direction.
limit : int, optional Maximum number of consecutive NaNs to fill. Must be greater than 0.

print(pd.Series(arr).interpolate(method = 'linear', limit_direction = 'forward', limit = 1))
#5 won't  get printed
print(pd.Series(arr).interpolate(method = 'linear', limit_direction = 'backward', limit = 1))
#3 won't get printed

You can try different variations based on your requirement.

Sign up to request clarification or add additional context in comments.

Comments

1

If possible use pandas create Series and then use Series.interpolate:

import pandas as pd
import numpy as np

arr = [1,np.NaN,np.NaN,7,np.NaN,27]

print (pd.Series(arr).interpolate().values)
[ 1.  3.  5.  7. 17. 27.]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.