问题描述
我正在尝试使用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,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')
请注意,索引本应是DatetimeIndex([...)而不是索引
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',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,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'],height=df.Height[df.Status=="Loss"],fill_color="red",line_color="black")
output_file("cs.html")
show(p)
我希望这对以后遇到同样困难的人有所帮助