首页 > 解决方案 > 熊猫插值不使用某些方法进行插值

问题描述

我有一个像这样的数据框:

[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 的方法工作正常(例如timeor linear),但是如果我尝试使用scipy类似kroghor的方法,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 插值方法起作用吗?

编辑:这是我正在处理的文件:链接

此外,这是我的玩具脚本,上面的 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` 版本,它会失败。

这也是我的玩具箱,它在真实数据上失败了:

标签: pythonpandasscipyinterpolation

解决方案


鉴于您的示例数据(使用 DatetimeIndex 格式化),所有可用的方法似乎都适用于 pandas 0.22.0:

                                      0       1
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

df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 5 entries, 2018-02-01 00:00:00 to 2018-02-01 02:00:00
Data columns (total 2 columns):
0    5 non-null datetime64[ns]
1    4 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 120.0 bytes

methods = ['linear', 'time', 'index', 'values', 'nearest', 'zero',
           'slinear', 'quadratic', 'cubic', 'barycentric', 'krogh', 
           'piecewise_polynomial', 'from_derivatives', 'pchip', 'akima']

for method in methods:
    print(method)
    print(df.interpolate(limit_direction='both', method=method))

linear
                                      0       1
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  368.28
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
                                      0       1
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  368.28
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
index
                                      0       1
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  368.28
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
values
                                      0       1
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  368.28
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
nearest
                                      0       1
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  435.24
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
zero
                                      0       1
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  435.24
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
slinear
                                      0       1
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  368.28
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
quadratic
                                      0           1
2018-02-01 00:00:00 2018-02-01 00:00:00  435.240000
2018-02-01 00:30:00 2018-02-01 00:30:00  361.818947
2018-02-01 01:00:00 2018-02-01 01:00:00  301.320000
2018-02-01 01:30:00 2018-02-01 01:30:00  256.680000
2018-02-01 02:00:00 2018-02-01 02:00:00  245.520000
cubic
                                      0       1
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  365.49
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
barycentric
                                      0       1
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  245.52
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
krogh
                                      0       1
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  365.49
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
piecewise_polynomial
                                      0       1
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  368.28
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
from_derivatives
                                      0       1
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  368.28
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
pchip
                                      0          1
2018-02-01 00:00:00 2018-02-01 00:00:00  435.24000
2018-02-01 00:30:00 2018-02-01 00:30:00  360.92087
2018-02-01 01:00:00 2018-02-01 01:00:00  301.32000
2018-02-01 01:30:00 2018-02-01 01:30:00  256.68000
2018-02-01 02:00:00 2018-02-01 02:00:00  245.52000
akima
                                      0             1
2018-02-01 00:00:00 2018-02-01 00:00:00  4.352400e+02
2018-02-01 00:30:00 2018-02-01 00:30:00 -5.045003e+07
2018-02-01 01:00:00 2018-02-01 01:00:00  3.013200e+02
2018-02-01 01:30:00 2018-02-01 01:30:00  2.566800e+02
2018-02-01 02:00:00 2018-02-01 02:00:00  2.455200e+02

推荐阅读