【问题标题】:AttributeError: 'RangeIndex' object has no attribute 'inferred_freq'AttributeError:“RangeIndex”对象没有属性“inferred_freq”
【发布时间】:2020-09-23 09:59:41
【问题描述】:

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

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

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

但我收到错误消息

AttributeError: 'RangeIndex' object has no attribute 'inferred_freq'

你能帮我解决这个问题吗

【问题讨论】:

    标签: python-3.x forecasting


    【解决方案1】:

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

    例如:

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

    注意到有一个属性freq='D',这意味着Pandas推断Pandas系列是每天(D=Daily)被索引的。

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

    # Convert your daily column from just string to DateTime (skip if already done)
    ts_log['Date'] = pd.to_datetime(ts_log['Date'])
    # Set the column 'Date' as index (skip if already done)
    ts_log = ts_log.set_index('Date')
    # Specify datetime frequency
    ts_log = ts_log.asfreq('D')
    

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

    【讨论】:

      【解决方案2】:

      对于statsmodel==0.10.1 并且ts_log 不是数据框或没有日期时间索引的数据框,请使用以下内容

      decomposition = seasonal_decompose(ts_log, freq=1)
      

      【讨论】:

        猜你喜欢
        • 2019-02-03
        • 2019-09-08
        • 2012-12-01
        • 2021-04-19
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 1970-01-01
        相关资源
        最近更新 更多