Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active October 22, 2021 13:00
Show Gist options
  • Save kidpixo/57aaef136bde7aa4f4fbca54eef409a3 to your computer and use it in GitHub Desktop.
Save kidpixo/57aaef136bde7aa4f4fbca54eef409a3 to your computer and use it in GitHub Desktop.
Interpolate a Pandas.DataFrame to an input index, drop duplicated index, with scipy.interpolate methods.
def reindex_and_interpolate(df,
new_index,
method,
extrapolation_warnings=True):
"""Interpolate an input Pandas.DataFrame to an input index.
Uses scipy.interpolate methods > https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
Handles extrapolation before and after the original X.
Works only for pandas version >=0.23.0.
Adapted from https://stackoverflow.com/a/51535881/1435167
Parameters
----------
df : pandas.DataFrame
df input pandas.DataFrame, index need to be numbers.
new_index : Index or array-like
new index, anthing that pandas.Index.union accepts.
method : str
one of scipy.interpolate method
extrapolation_warnings : Bool
raise a warnings if extrapolation happens (new_index "bigger" than df.index)
"""
kw = dict(method=method, fill_value="extrapolate",limit_area=None,limit_direction='both')
# add new index, interpolate on Nan values on new_index,select only new_index
out_df = df.reindex(df.index.union(new_index)).interpolate(**kw).loc[new_index]
# drop duplicated indexes
out_df = out_df.loc[out_df.index.drop_duplicates(keep='first')]
return out_df
## simple reindexing example
# test dataframe
f = pd.DataFrame(data=[0.0,3.0,2.0,7.0,3.5,6.0],columns=['a'],index=np.arange(6))
ax = f.rename(columns={'a':'original'}).plot(marker='.',markersize=30,linewidth=6,color='black',figsize=[10,4])
new_index = np.concatenate([[-0.25],f.index.to_numpy()+0.25])
f_reindex_ls = [
reindex_and_interpolate(f, new_index ,method=method).rename(columns={'a':method})
for method in ['nearest', 'zero', 'slinear', 'quadratic', 'cubic']
]
f_reindex = pd.concat(f_reindex_ls,axis=1)
f_reindex.plot.line(marker='.',ax=ax,markersize=20,linewidth=4)
print(f)
print(f_reindex)
plt.tight_layout()
plt.savefig('output.png',dpi=100,facecolor='white', transparent=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment