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!

Shaheer
1,4133 gold badges13 silver badges29 bronze badges
asked Nov 10, 2019 at 9:01

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.

answered Nov 10, 2019 at 9:39
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.]
answered Nov 10, 2019 at 9:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.