Pandas插值不是用某些方法插值的

2024-04-20 04:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个数据帧:

[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的基本方法工作得很好(例如timelinear),但是如果我尝试使用scipy方法,比如kroghbarycentric,我发现插值并不能插值这些点:

^{pr2}$

我的插值方法如下:

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`版本,它将失败。在

另外,我的玩具箱在真实数据上失败了:


Tags: to数据方法dfindextypenanmethod
1条回答
网友
1楼 · 发布于 2024-04-20 04:20:19

给定您的示例数据(使用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

相关问题 更多 >