iT邦幫忙

1

使用 python arima 預測跨月分資料時有疑問, 問題: .to_period('B') 或 .to_period('D')

  • 分享至 

  • twitterImage
# -*- coding: utf-8 -*-
import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
from datetime import datetime,timedelta,date,time
import warnings
warnings.filterwarnings('ignore')
filename = '03.xlsx'
df = pd.read_excel(filename,header=0,parse_dates=True,index_col=0,squeeze=True)
d = pd.date_range(df.index.min(),df.index.max())
res = pd.Series(df['Close'],d)
res = res.dropna()
print('-----original data-----')

res.index = pd.DatetimeIndex(res.index).to_period('B')

print(res)
r = ARIMA(res,(1,2,0))
model_fit = r.fit(disp=0)
afew = 122
pred = model_fit.predict(start = df.index.max()+timedelta(0), end = df.index.max()+timedelta(afew),typ='levels',dynamic =True)
print('-----'+ str(afew) +' day forecast predict data-----\n'+str(pred))

有 res.index = pd.DatetimeIndex(res.index).to_period('B') 這行就可行
https://ithelp.ithome.com.tw/upload/images/20210227/20109318E1VaY1qxFJ.png
沒有 res.index = pd.DatetimeIndex(res.index).to_period('B') 這行就報錯
https://ithelp.ithome.com.tw/upload/images/20210227/20109318Lm6u6QkPaV.png
主要錯:
一、 only integers slices (:) ellipsis (...) numpy.newaxis (None) and integer or boolean arrays are valid indices
二、 The end argument could not be matched to a location related to the index of the data
不好意思打擾各位大大了!請問各位大大知道為什麼會這樣嗎?謝謝!

ccutmis iT邦高手 2 級 ‧ 2021-02-27 13:22:00 檢舉
股市有句話叫作「能預知三日行情則富可敵國。」這個主題個人沒試過,也完全不想試,用歷史行情去預測未來是玄學不是科學...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
Alien
iT邦新手 4 級 ‧ 2021-02-27 14:08:00

.to_period('B')的B是business days
The end argument could not be matched to a location related to the index of the data參數和index位置無法匹配
你原始的時間資料不是business days
當然無法model.fit

不過我不知道你的model
也不確定你的資料是什麼
所以我大概猜是這樣

參考資料:https://pandasguide.readthedocs.io/en/latest/Pandas/timeseries.html

@clash110502
您好!
我的 model 是 arima
我的資料是來自 yfinance 個股的收盤價
謝謝!

我要發表回答

立即登入回答