用 Pandas 插值 CubicSpline

Interpolate CubicSpline with Pandas

我有一个包含 ResidMat 和 Price 的数据框,我使用 scipy 来查找插值 CubicSpline。我使用 CubicSpline 并申请在我的数据集中查找所有数据。但它不是很快,因为在这种情况下没有更多的数据。我将有一百多个数据,而且速度很慢。您是否有这样做的想法,但可能使用矩阵?

谢谢,

    def add_interpolated_price(row, generic_residmat):
        from scipy.interpolate import CubicSpline
        residmats = row[['ResidMat']].values
        prices = row[['Price']].values
        cs = CubicSpline(residmats, prices)
        return float(cs(generic_residmat))

    df = pd.DataFrame([[1,18,38,58,83,103,128,148,32.4,32.5,33.8,33.5,32.8,32.4,32.7],[2,17,37,57,82,102,127,147,31.2,31.5,32.7,33.2,32.5,32.9,33.3]],columns = ['index','ResidMat','ResidMat','ResidMat','ResidMat','ResidMat','ResidMat','ResidMat','Price','Price','Price','Price','Price','Price','Price'],index=['2010-06-25','2010-06-28'])
    my_resimmat = 30
    df['Generic_Value'] =  df.apply(lambda row: add_interpolated_price(row, generic_residmat=my_resimmat), axis=1)

在查看了这段代码的概况后,大部分时间都花在了插值上,所以我建议最好的办法是使用 pandarallel。 有详细信息。我最喜欢这个方法...(下面的大纲代码)

from pandarallel import pandarallel
from math import sin

pandarallel.initialize()

def func(x):
    return sin(x**2)

df.parallel_apply(func, axis=1)

但这仅适用于 Linux 和 Macos,适用于 Windows,仅当 Python 会话从 Windows 子系统执行 [=19] 时,Pandarallel 才有效=] (WSL).