【发布时间】:2018-12-08 21:33:49
【问题描述】:
我有一个这样的数据框:
[5232 rows x 2 columns]
0 2
0
2018-02-01 00:00:00 2018-02-01 00:00:00 435.24
2018-02-01 00:30:00 2018-02-01 00:30:00 NaN
2018-02-01 01:00:00 2018-02-01 01:00:00 301.32
2018-02-01 01:30:00 2018-02-01 01:30:00 256.68
2018-02-01 02:00:00 2018-02-01 02:00:00 245.52
我正在尝试对其进行插值。我发现熊猫的基本方法工作正常(例如time 或linear),但如果我尝试使用scipy 方法如krogh 或barycentric 我发现插值不会出现插值:
0 2
0
2018-02-01 00:00:00 2018-02-01 00:00:00 435.24
2018-02-01 00:30:00 2018-02-01 00:30:00 NaN
2018-02-01 01:00:00 2018-02-01 01:00:00 301.32
2018-02-01 01:30:00 2018-02-01 01:30:00 256.68
2018-02-01 02:00:00 2018-02-01 02:00:00 245.52
我的插值方法如下:
def interpolate(df : DataFrame, interpolate_type : str = 'pandas'):
""" Helper method for inserting different interpolation methods into the main function. """
if interpolate_type == 'pandas':
return df.interpolate(limit_direction='both', method='time')
if interpolate_type == 'krogh':
return df.interpolate(limit_direction='both', method='krogh')
您还需要做些什么才能使 scipy 插值方法起作用吗?
编辑:这是我正在处理的文件:link
另外,这是我的玩具脚本,上面的 CSV 失败:
df_2[2] = pd.to_numeric(df_2[2],errors='force')
df_2 = df_2.set_index(pd.DatetimeIndex(df_2[0])) # Increases interpolation accuracy.
df_2.index = pd.to_datetime(df_2.index)
df_2.iloc[1, 2] = np.NaN
df_2.sort_index(inplace=True)
print(df_2.interpolate(limit_direction='both', method='krogh'))
如果我更改 interpolate function to anyscipy` 版本,它会失败。
这也是我的玩具箱在真实数据上失败了:
【问题讨论】:
-
在您的玩具示例中,您的索引不是由日期时间组成,而是由字符串组成,因此会出现错误消息。在
df_2.index = pd.to_datetime(df_2.index)之后仍然失败吗? -
啊!不,它没有进步!
-
除了这个小例子之外,我无法让它与其他任何东西一起使用,我已经上传了我正在使用的文件之一。
标签: python pandas scipy interpolation