def get_interpolator(self, xcolumn, ycolumn, extrap='nearest'):
"""
Get an interpolator instance between the two columns
Parameters:
===========
- xcolumn: string
The name of the x column to interpolate between
- ycolumn: string
The name of the value you want to interpolate
- extrap: string
How to treat extrapolation. Options are:
1. 'nearest': Default behavior. It will return the nearest match
to the given 'x' value
2. 'extrapolate': Extrapolate the spline. This is probably only
safe for very small extrapolations
Returns:
========
A callable interpolator.
"""
# Make sure the column names are correct
assert xcolumn in self.mam_df.keys() and ycolumn in self.mam_df.keys()
# Sort the dataframe by the x column, and drop any duplicates or nans it might have
sorted_df = self.mam_df.sort_values(by=xcolumn).dropna(subset=[xcolumn, ycolumn], how='any').drop_duplicates(xcolumn)
# Make an interpolator
ext_value = {'nearest': 3, 'extrapolate': 0}
fcn = spline(sorted_df[xcolumn].values, sorted_df[ycolumn].values, ext=ext_value[extrap])
return fcn
评论列表
文章目录