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 的方法工作正常(例如time
or linear
),但是如果我尝试使用scipy
类似krogh
or的方法,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 any
scipy` 版本,它会失败。
这也是我的玩具箱,它在真实数据上失败了:
解决方案
鉴于您的示例数据(使用 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
推荐阅读
- python - 如何使用递归 ANTLR4 解析树侦听器构建 JSON
- android - 在 android studio 中更新 gradle 后,矢量可绘制图标的混乱
- javascript - 在本地不可变地存储一个 jQuery 对象以供以后使用
- azure - 在流量管理器和 Web 应用程序上配置自定义域
- python-2.7 - 添加选项 -w 时出现 Pyinstaller 错误
- c# - 在 TableLayout 中隐藏一行
- android - 程序类型已经存在:android.support.design.widget.CoordinatorLayout$Behavior
- python-3.x - 在 sqlalchemy 中,反映来自 MSSQL 的元数据并应用于 ORACLE 错误
- tensorflow - 使用 Tensorflow 对象检测 API 进行迁移学习时,如何更改隐藏层的数量?
- r - 有效地从矩阵列中减去