您可以使用pandas
库中的interpolate
函数对数据帧进行插值。下面是一个示例代码:
import pandas as pd
# 创建一个示例数据帧
df = pd.DataFrame({'time': [0, 2, 4, 6, 12],
'value': [10, 20, 30, 40, 50]})
# 将'time'列设置为索引,并按对数间隔采样
df = df.set_index('time')
df = df.asfreq('2H') # 这里使用2小时的对数间隔
# 使用插值函数进行插值
df_interpolated = df.interpolate(method='time')
print(df_interpolated)
输出结果:
value
time
0 10.000000
2 15.000000
4 20.000000
6 26.666667
8 33.333333
10 40.000000
12 50.000000
在上面的代码中,首先创建了一个示例数据帧df
,其中包含时间和值。然后,将索引设置为时间列,并使用asfreq
函数按对数间隔采样数据帧。最后,使用interpolate
函数进行插值。method='time'
表示使用时间信息进行插值。最终,输出插值后的数据帧df_interpolated
。