Pandas如何用向量化方式利用梯度向前填充含NaN的序列?
用前序已知梯度填充DataFrame的NaN缺口
现有一个包含price序列的DataFrame,该序列存在含NaN的缺口:
import numpy as np import pandas as pd df = pd.DataFrame({"price": [1, 2, 3, np.nan, np.nan, np.nan, np.nan, np.nan, 9, 10]}, index=pd.date_range("2023-01-01", periods=10))
对应原始数据:
price 2023-01-01 1.0 2023-01-02 2.0 2023-01-03 3.0 2023-01-04 NaN 2023-01-05 NaN 2023-01-06 NaN 2023-01-07 NaN 2023-01-08 NaN 2023-01-09 9.0 2023-01-10 10.0
期望结果
希望用缺口前的已知梯度(此处为连续递增1)填充这些NaN,得到如下结果:
price 2023-01-01 1.0 2023-01-02 2.0 2023-01-03 3.0 2023-01-04 4.0 2023-01-05 5.0 2023-01-06 6.0 2023-01-07 7.0 2023-01-08 8.0 2023-01-09 9.0 2023-01-10 10.0
现有低效实现
目前可以通过迭代实现该需求,但效率较低:
gradients = (df["price"] - df["price"].shift(1)).ffill() price_values = df["price"].values for index, val in enumerate(price_values): last_price = price_values[index - 1] gradient = gradients.iloc[index] if pd.isna(val) and not pd.isna(last_price) and not pd.isna(gradient): df["price"].iat[index] = last_price + gradient
问题
推测Pandas可能内置相关功能但未找到,请问是否有更优的向量化实现方式?
内容的提问来源于stack exchange,提问作者Student
相关产品推荐
相关产品推荐

