绘制熊猫系列数据的平滑曲线

发布于 2021-01-29 14:09:50

我的数据是:

>>> 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。

关注者
0
被浏览
199
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    得到它了。在这个问题的帮助下,我做了以下工作:

    1. 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


    2. 使用插值数据.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

    3. 使用绘制它tsint.plot()。这是原始版本tsgroup和的比较tsint

    1如果由于.interpolate(method='cubic')告诉您即使已安装Scipy也未安装而出现错误,请打开/usr/lib64/python2.6/site- packages/scipy/interpolate/polyint.py文件或将文件放在任何位置,然后将第二行从更改from scipy import factorialfrom scipy.misc import factorial



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看