【问题标题】:Time Series using python year 0 is out of range使用python第0年的时间序列超出范围
【发布时间】:2019-08-01 07:29:43
【问题描述】:

我正在使用 python 进行时间序列分析。我看了从雅虎财经下载的csv文件,Yahoo Apple historical data

    aapl_df = pd.read_csv('AAPL.csv',
                     parse_dates=['Date'], 
                     index_col='Date'
                     )
    aapl_df.head()
        # Simple Exponential Smoothing
    adj_price = pd.Series(aapl_df['Adj Close'])
    fit1 = SimpleExpSmoothing(adj_price).fit(smoothing_level=0.2,optimized=False)
    fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$')
    # plot
    fcast1.plot(marker='o', color='blue', legend=True)
    fit1.fittedvalues.plot(marker='o',  color='blue')

当我使用 SimpleExpSmoothing 和 Holt 库时,

出现错误

/home/jupyterlab/conda/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py:225: ValueWarning: A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting.
  ' ignored when e.g. forecasting.', ValueWarning)
/home/jupyterlab/conda/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py:531: ValueWarning: No supported index is available. Prediction results will be given with an integer index beginning at `start`.
  ValueWarning)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-740724523405> in <module>
      4 # plot
      5 fcast1.plot(marker='o', color='blue', legend=True)
----> 6 fit1.fittedvalues.plot(marker='o',  color='blue')
      7 
      8 

~/conda/lib/python3.6/site-packages/matplotlib/dates.py in tick_values(self, vmin, vmax)
   1408         ymax = self.base.ge(vmax.year) * self.base.step
   1409 
-> 1410         ticks = [vmin.replace(year=ymin, **self.replaced)]
   1411         while True:
   1412             dt = ticks[-1]

ValueError: year 0 is out of range

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 分享一点数据,问题会更容易回答。

标签: python date error-handling time-series prediction


【解决方案1】:

您似乎没有将索引设为Datetime。尽管您已经完成了parse_dates,但它仍然保留为字符串索引。您应该将其转换为DatetimeIndex 并通过to_period 传递频率, 试试这个,

aapl_df .index = pd.DatetimeIndex(aapl_df .index).to_period('D')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2016-12-16
    • 2016-03-31
    • 2020-04-04
    • 2015-03-03
    • 1970-01-01
    相关资源
    最近更新 更多