绘制熊猫系列数据的平滑曲线
我的数据是:
>>> ts = pd.TimeSeries(data,indexconv)
>>> tsgroup = ts.resample('t',how='sum')
>>> tsgroup
2014-11-08 10:30:00 3
2014-11-08 10:31:00 4
2014-11-08 10:32:00 7
[snip]
2014-11-08 10:54:00 5
2014-11-08 10:55:00 2
Freq: T, dtype: int64
>>> tsgroup.plot()
>>> plt.show()
indexconv
是使用转换的字符串datetime.strptime
。
这样的情节非常前卫(这些不是我的实际情节):
我如何像这样平滑它:
我知道本文中scipy.interpolate
提到的内容(这是我从中获取图像的地方),但是如何将其应用于熊猫时间序列?
我发现了一个名为Vincent的很棒的库,它可以处理Pandas,但它不支持Python 2.6。
-
得到它了。在这个问题的帮助下,我做了以下工作:
-
tsgroup
从几分钟到几秒重新采样。\ >>> tsres = tsgroup.resample('S')
\ >>> tsres
2014-11-08 10:30:00 3
2014-11-08 10:30:01 NaN
2014-11-08 10:30:02 NaN
2014-11-08 10:30:03 NaN
…
2014-11-08 10:54:58 NaN
2014-11-08 10:54:59 NaN
2014-11-08 10:55:00 2
频率:S,长度:1501 -
使用插值数据
.interpolate(method='cubic')
。这会将数据传递给scipy.interpolate.interp1d
使用cubic
类型,因此您需要安装scipy(pip install scipy
)1。\ >>> tsint = tsres.interpolate(method ='cubic')
\ >>> tsint
2014-11-08 10:30:00 3.000000
2014-11-08 10:30:01 3.043445
2014-11-08 10:30:02 3.085850
2014-11-08 10:30:03 3.127220
…
2014-11-08 10:54:58 2.461532
2014-11-08 10:54:59 2.235186
2014-11-08 10:55:00 2.000000
频率:S,长度:1501 -
使用绘制它
tsint.plot()
。这是原始版本tsgroup
和的比较tsint
:
1如果由于
.interpolate(method='cubic')
告诉您即使已安装Scipy也未安装而出现错误,请打开/usr/lib64/python2.6/site- packages/scipy/interpolate/polyint.py
文件或将文件放在任何位置,然后将第二行从更改from scipy import factorial
为from scipy.misc import factorial
。 -