时间序列中的样条插值

时间:2018-06-04 22:37:36

标签: python interpolation spline

我有以下数据。

date        value
2018-06-04  191.790000
2016-05-13  87.454124
2015-11-06  115.626664
2015-08-21  100.580815
2015-05-22  125.482610
2013-12-27  73.598623
2013-04-19  50.347372
2012-09-21  89.324522
2011-11-25  46.190391
2011-06-17  40.687995
2010-08-27  30.697038
2010-06-18  34.820214
2009-01-16  10.459760
2007-12-28  25.387754
2006-07-14  6.437459
2006-01-13  10.873919
2005-02-25  5.652946
2004-06-25  2.140738
2003-12-19  1.251411

数据类型为:

In: high_low.info()
Out:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 19 entries, 2018-06-04 to 2003-12-19
Data columns (total 1 columns):
value    19 non-null float64
dtypes: float64(1)
memory usage: 944.0 bytes

我想使用Spline Interpolation:

x = high_low.index
y = high_low['value']
interp1d(x, y, kind='cubic')

但如果我尝试运行它,它会给我以下错误消息:

> --> 512                 if np.isnan(self.x).any():
> 
> TypeError: ufunc 'isnan' not supported for the input types, and the
> inputs could not be safely coerced to any supported types according to
> the casting rule ''safe''

有没有人有想法修复此问题? 提前谢谢。

编辑: 正如KRKirov建议的那样,我试图通过添加以下内容将数据类型从datetime64 [ns]转换为浮点数:

In: high_low.index.values.astype('d')
Out: array([1.5280704e+18, 1.4630976e+18, 1.4467680e+18, 1.4401152e+18,
   1.4322528e+18, 1.3881024e+18, 1.3663296e+18, 1.3481856e+18,
   1.3221792e+18, 1.3082688e+18, 1.2828672e+18, 1.2768192e+18,
   1.2320640e+18, 1.1988000e+18, 1.1528352e+18, 1.1371104e+18,
   1.1092896e+18, 1.0881216e+18, 1.0717920e+18])

通过绘制值:

f2 = interp1d(x, y, kind='cubic')
x = high_low.index.values.astype('d')
y = high_low['value']
plt.plot(x,f2(x),color='green')
scatter(high_low.index.values.astype('d'),high_low['value'],color='red')

我得到了:

spline

有没有人有更好的方法让x轴回到原来的datetimeindex类型?

0 个答案:

没有答案