【问题标题】:Python seasonal_decompose function from Statsmodels library giving ValueError来自 Statsmodels 库的 Pythonseason_decompose 函数给出 ValueError
【发布时间】:2018-10-10 16:05:45
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

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


【解决方案1】:

如果您有一个带有 DateTimeIndex 的 pandas 数据框,您可以检查它的频率属性。

print(df.index.freq)

如果返回None,您可以通过直接分配给df.index.freq 属性或使用df.asfreq() 方法来设置频率。例如。

# Set df freq to 120 min
df.index.freq = '120t'
df.asfreq('120t')

使用df.asfreq() 将允许您轻松地在频率之间重新采样/转换并处理重新采样。您可以找到有效偏移别名列表(代码的“t”部分)here。或者您可以运行 pd.tseries.offsets.__all__ 来仅获取有效偏移量列表(注意,这不会打印别名,因此您将看到 Minute 而不是 mint)。

一旦您的数据框具有有效时间,您就可以运行seasonal_decompose()。在statsmodels 0.12.2 中进行实验,频率超过 1 小时即freq '60T',导致ValueError: freq T not understood. Please report if you think this is in error. 被抛出。如果这意味着您需要对df 进行下采样,我建议您使用以下df.resample('1H').median() 的方法。

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多