了解pandas插值函数

时间:2016-12-11 13:09:55

标签: pandas scipy interpolation

我见过很多比较,例如this,但我仍然不明白,因为我没有与日期时间和浮点精度相关的问题。

我在理解pandas中插值函数的实现时遇到了问题。

我正在尝试以下方法:

import pandas as pd
df = pd.DataFrame({'MC':[1,2,4,6,7,8,9,10,12,13,15], 'MW':[1,2,4,6,7,8,9,10,12,13,15]})
df.set_index('MW', inplace=True)
df2 = df.reindex([1,2,4,6,7,8,9,10,12,13,15,3,5,11,13,14,16])

df2.apply(pd.Series.interpolate)
      MW
MC      
1    1.0
2    2.0
4    4.0
6    6.0
7    7.0
8    8.0
9    9.0
10  10.0
12  12.0
13  13.0
15  15.0
3   14.5
5   14.0
11  13.5
13  13.0
14  13.0
16  13.0

对于插值和外推而言,这看起来非常不寻常。这里没有浮点问题。

当我使用scipy插值时,我得到 期望的结果 ,但需要实现单独的推断:

from scipy import interpolate
f = interpolate.interp1d(df.index, df.values.flatten())
f([3,5,11,13,14])
array([  3.,   5.,  11.,  13.,  14.])

编辑:我尝试了以下众多选项:

df2.interpolate(method='index')
      MW
MC      
1    1.0
2    2.0
4    4.0
6    6.0
7    7.0
8    8.0
9    9.0
10  10.0
12  12.0
13  13.0
15  15.0
3    3.0
5    5.0
11  11.0
13  13.0
14  13.0
16  13.0

0 个答案:

没有答案