Python 使用插值重新索引数据帧

Python 使用插值重新索引数据帧,python,pandas,dataframe,numpy,Python,Pandas,Dataframe,Numpy,我有一个带有DateTimeIndex和“阈值”、“路径”列的熊猫数据帧: 路径阈值 2020-12-11 04:00:25.729 0.000104 -1.107422 2020-12-11 04:00:25.731 0.000387 -1.107422 2020-12-11 04:00:25.733 0.000899 -1.107422 2020-12-11 04:00:25.735 0.001561 -1.117676 2020-12-11 04:00:25.737 0.

我有一个带有DateTimeIndex和“阈值”、“路径”列的熊猫数据帧:

路径阈值
2020-12-11 04:00:25.729  0.000104  -1.107422
2020-12-11 04:00:25.731  0.000387  -1.107422
2020-12-11 04:00:25.733  0.000899  -1.107422
2020-12-11 04:00:25.735  0.001561  -1.117676
2020-12-11 04:00:25.737  0.002272  -1.117676
...                           ...        ...
2020-12-11 04:01:03.063  9.085985  -1.209961
2020-12-11 04:01:03.065  9.085985  -1.209961
2020-12-11 04:01:03.067  9.085985  -1.209961
2020-12-11 04:01:03.069  9.085985  -1.199707
2020-12-11 04:01:03.071  9.085985  -1.199707
现在我想创建一个新的数据帧,它在一个线性间隔版本的“Path”上建立索引,即

np.arange(df[“Path”].min(),df[“Path”].max(),0.05) 阵列([1.04000E-04,5.010400e-02,1.001040e-01,1.501040e-01, 2.001040e-01,2.501040e-01。。。 “Path”中的值是单调的(但不是严格单调的)。作为这个新数据帧的一列,我想从“Threshold”中设置适当的插值然而,使用pandas的
interpolate
和numpy的
interp
方法,我没有做到这一点。有没有办法做到这一点?

一个想法是使用:

另一个想法与删除重复和:


如果只使用了问题中的样本数据,那么预期的输出是如何的?在
路径
中重复的值会发生什么情况?@jezrael这对我来说并不重要,我想只要从
路径
中删除任何重复项就可以了,而你的两个想法都有效。你的第二个想法让我更接近我需要继续的内容。谢谢!
a = np.arange(df["Path"].min(), df["Path"].max(), 0.05)

df1 = pd.merge_asof(df.reset_index(), 
                    pd.DataFrame({'new':a}), 
                    left_on='Path', 
                    right_on='new', 
                    direction='nearest')
df2 = (df.drop_duplicates('Path')
         .reset_index()
         .set_index('Path')
         .reindex(a, method='nearest'))