我有这样一个数据帧:
[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
我试着插进去。我发现panda的基本方法工作得很好(例如time
或linear
),但是如果我尝试使用scipy
方法,比如krogh
或barycentric
,我发现插值并不能插值这些点:
我的插值方法如下:
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 any
scipy`版本,它将失败。在
另外,我的玩具箱在真实数据上失败了:
给定您的示例数据(使用DatetimeIndex格式化),所有可用方法似乎都适用于pandas 0.22.0:
相关问题 更多 >
编程相关推荐