[英]BokehUserWarning and problem with pandas_datareader in python
我正在嘗試使用 pandas_datareader 模塊從 data.DataReader 獲取的股票數據繪制帶有 bokeh.plotting 的圖表。
問題一:檢索到的數據在 pandas.Index 而不是 pandas.DatetimeIndex
問題二:我收到 BokehWarning
代碼1:
import os
from pandas_datareader import data
from datetime import datetime as dt
from bokeh.plotting import figure, show, output_file
os.environ["ALPHAVANTAGE_API_KEY"] = "E____secret____4"
hours_12 = 12*60*60*1000
start = dt(2016,3,1)
end = dt(2016,3,10)
f = data.DataReader('GOOG','av-daily',start,end,api_key=os.getenv('ALPHAVANTAGE_API_KEY'))
#f
#f.loc['2016-03-09']
f.index
輸出:
Index(['2016-03-01', '2016-03-02', '2016-03-03', '2016-03-04', '2016-03-07',
'2016-03-08', '2016-03-09', '2016-03-10'],
dtype='object')
請注意, index 應該是 DatetimeIndex([...) 而不是 Index
f 的值為:
open high low close volume
2016-03-01 703.62 718.8100 699.77 718.81 2151419
2016-03-02 719.00 720.0000 712.00 718.85 1629003
2016-03-03 718.68 719.4500 706.02 712.42 1957974
2016-03-04 714.99 716.4900 706.02 710.89 1972077
2016-03-07 706.90 708.0912 686.90 695.16 2988026
2016-03-08 688.59 703.7900 685.34 693.97 2058471
2016-03-09 698.47 705.6800 694.00 705.24 1421515
2016-03-10 708.12 716.4400 703.36 712.82 2833525
代碼 2:
p = figure(x_axis_type='datetime',width=1000,height=400, title="CandleStick Chart")
p.rect(x=f.index[f.close > f.open],y=(f.open + f.close)/2, width=hours_12, height=abs(f.open-f.close))
output_file("cs.html")
show(p)
輸出:
BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 4), ('y', 8)
BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('height', 8), ('x', 4), ('y', 8)
散景輸出是一個空白的石板,它不應該是。 我遵循了一個教程,它似乎在教程中起作用(雖然教程有點過時)請幫忙,並對答案保持溫和。 一直很頭疼
我通過將數據源從 Alpha Vantage 更改為 Yahoo Finance 找到了解決方案。 我沒有直接使用 yahoo 作為 data.DataReader 模塊的源,因為它已被棄用。 結果是雅虎金融創建了一個名為 yfinance 的單獨模塊。
from pandas_datareader import data
from datetime import datetime as dt
import yfinance as yf
yf.pdr_override() #this enables your code use yfinance instead of pdr
from bokeh.plotting import figure, show, output_file
hours_12 = 12*60*60*1000
start = dt(2016,3,1)
end = dt(2016,3,10)
df=data.get_data_yahoo(tickers="GOOG", start=start, end=end)
yfinance 通過 alpha vantage 創建 pandas.DatetimeIndex 而不是 pandas.Index 數據。 這會創建圖形,但仍然存在 BokehWarning
為了解決 BokehWarning 問題並確保您的圖表准確且一致,我通過執行此操作在 df 中創建了新列
def status(open,close):
if open < close:
value="Profit"
elif open > close:
value="Loss"
else: value="Equal"
return value
df['Status'] = [status(open,close) for open,close in zip(df.Open,df.Close)]
df['Middle_y'] = (df.Open+df.Close)/2
df['Height'] = abs(df.Close-df.Open)
然后,您可以繼續通過添加變量來創建圖表。
p = figure(x_axis_type='datetime',width=1000,height=400, title="CandleStick Chart")
p.rect(x=df.index[df.Status =="Profit"],y=df.Middle_y[df.Status=='Profit'], width=hours_12,
height=df.Height[df.Status=="Profit"],fill_color="green", line_color="black")
p.rect(x=df.index[df.Status =="Loss"],y=df.Middle_y[df.Status=='Loss'], width=hours_12,
height=df.Height[df.Status=="Loss"],fill_color="red", line_color="black")
output_file("cs.html")
show(p)
我希望這可以幫助將來遇到同樣困難的任何人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.