来自Statsmodels库的Python seasonal_decompose函数给出了ValueError

时间:2018-04-30 14:27:02

标签: python python-3.x time-series statsmodels

我正在尝试使用Statsmodels库中的seasonal_decompose函数来分解我的时间序列数据的季节性和趋势,但我得到了ValueError

数据:

                    A (Current average)
     TS 
2017-12-01 00:01:00 3.274965
2017-12-01 00:02:00 3.274083
2017-12-01 00:03:00 3.262563
2017-12-01 00:04:00 3.278352
2017-12-01 00:05:00 3.251769

数据索引:

ts_log.index

输出:

DatetimeIndex(['2017-12-01 00:01:00', '2017-12-01 00:02:00',
               '2017-12-01 00:03:00', '2017-12-01 00:04:00',
               '2017-12-01 00:05:00', '2017-12-01 00:06:00',
               '2017-12-01 00:07:00', '2017-12-01 00:08:00',
               '2017-12-01 00:09:00', '2017-12-01 00:10:00',
               ...
               '2018-01-04 23:26:00', '2018-01-04 23:27:00',
               '2018-01-04 23:28:00', '2018-01-04 23:29:00',
               '2018-01-04 23:30:00', '2018-01-04 23:31:00',
               '2018-01-04 23:32:00', '2018-01-04 23:33:00',
               '2018-01-04 23:34:00', '2018-01-04 23:35:00'],
              dtype='datetime64[ns]', name='TS', length=50000, freq=None)

分解季节性

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts_log)

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-79-7ca5a90bdbf8> in <module>()
      1 from statsmodels.tsa.seasonal import seasonal_decompose
----> 2 decomposition = seasonal_decompose(ts_log)
      3 
      4 trend = decomposition.trend
      5 seasonal = decomposition.seasonal

C:\Users\Paras Mani\Anaconda2\envs\py3\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided)
     82             freq = pfreq
     83         else:
---> 84             raise ValueError("You must specify a freq or x must be a "
     85                              "pandas object with a timeseries index with"
     86                              "a freq not set to None")

ValueError: You must specify a freq or x must be a pandas object with a timeseries index witha freq not set to None

这里我使用的是时间序列索引,但频率设置为无。我怎样才能改变频率。

1 个答案:

答案 0 :(得分:-1)

尝试一下:

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(x, model='additive', filt=None, freq=52)
fig = decomposition.plot()
plt.show()