【发布时间】:2020-08-27 07:31:08
【问题描述】:
我正在尝试使用 pandas_datareader 模块从 data.DataReader 获取的股票数据中使用散景绘制图表。
问题一:检索到的数据在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')
请注意,索引是 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', 4), ('y', 8)
散景输出是一张白纸,不应该是这样。我遵循了一个教程,它似乎在教程中工作(虽然教程有点过时) 请帮忙,对答案要温和。一直很头疼
【问题讨论】:
-
您正在过滤进入
x的值,但您没有过滤进入y和height的值。这就是长度不同的原因。 -
@Eugene Pakhomov 感谢您的回复,我也注意到了;但是,我不知道为什么它在教程中起作用。(udemy.com/course/the-python-mega-course)具有相同的值。
-
我无法查看教程的代码,因为它位于付费墙后面。但是,如果这是 100% 相同的代码,那么这是一个糟糕的教程,它使用了恰好与某些特定数据一起工作的错误代码。
-
其实还不错,我就是不耐烦了,教程实际上帮我解决了后面视频中的大部分问题。我猜当时模块正在工作,现在它已被弃用。教程真的很有帮助,不想给我的老师带来麻烦。????
标签: python bokeh pandas-datareader