import numpy as np, pandas as pd
data = np.array([[[3, 2, 1, np.nan, np.nan],
[22, 1, 1, 4, 4],
[4, 2, 3, 3, 4],
[1, 1, 4, 1, 5],
[2, 4, 5, 2, 1]],
[[6, 7, 10, 6, np.nan],
[np.nan, 7, 8, 6, 9],
[6, 10, 9, 8, 10],
[6, 8, 7, 10, 8],
[10, 9, 9, 10, 8]],
[[6, 7, 10, np.nan, np.nan],
[19, 19, 8, 6, 9],
[6, 10, 9, 8, 10],
[6, 8, 7, 10, 8],
[10, 9, 9, 10, 8]],
[[6, 7, 10, 6, np.nan],
[19, 21, 8, 6, 9],
[6, 10, 9, 8, 10],
[6, 8, 7, 10, 8],
[10, 9, 9, 10, 8]],
[[12, 14, 12, 15, np.nan],
[19, 11, 14, 14, 11],
[13, 13, 16, 15, 11],
[14, 15, 14, 16, 14],
[13, 15, 11, 11, 14]]])
new_data = data.reshape(5,25)
df = pd.DataFrame(new_data)
result = df.interpolate(axis=0,method='cubic').values.reshape(data.shape)
print result
Though some locations has 4 non nan values, the whole process is stopped, saying the 'cubic' method requires at least 4 non nan values. How can I make it conditional to apply the 'cubic' method to change values for those locations which can run 'cubic' method?
new_data= data.reshape(-1, 5); pd.DataFrame(new_data).interpolate(axis=1, method='linear')np.ndarraydoingcond=new_data.shape[1] - np.sum(np.isnan(new_data), axis=1) >=4, and then creating the dataframe likepd.DataFrame(new_data[cond, :]), this will accept thecubicinterpolation, but in this case some of thenans still do not vanish...