AttributeError:“RangeIndex”对象没有属性“inferred_freq”

new*_*hon 4 forecasting python-3.x

我正在尝试在 python 3.x 中进行预测。所以我写了下面的代码

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

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息

AttributeError: 'RangeIndex' object has no attribute 'inferred_freq'
Run Code Online (Sandbox Code Playgroud)

你能帮我解决这个问题吗

Ale*_*ses 5

您需要确保您的 Panda Series 对象ts_log具有带有推断频率的 DateTime 索引。

例如:

ts_log.index
>>> DatetimeIndex(['2014-01-01', ... '2017-12-31'],
              dtype='datetime64[ns]', name='Date', length=1461, freq='D')
Run Code Online (Sandbox Code Playgroud)

注意到有一个属性freq='D',它意味着 Pandas 推断 Pandas Series 是 Daily 索引的(D=Daily)。

现在为了实现这一目标,我假设您的系列有一个名为“日期”的列。这是执行此操作的代码:

ts_log.index
>>> DatetimeIndex(['2014-01-01', ... '2017-12-31'],
              dtype='datetime64[ns]', name='Date', length=1461, freq='D')
Run Code Online (Sandbox Code Playgroud)

对于每日以外的频率,请参阅此处: https: //pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases