【问题标题】:BokehUserWarning and problem with pandas_datareader in pythonBokehUserWarning 和 python 中 pandas_datareader 的问题
【发布时间】: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 的值,但您没有过滤进入yheight 的值。这就是长度不同的原因。
  • @Eugene Pakhomov 感谢您的回复,我也注意到了;但是,我不知道为什么它在教程中起作用。(udemy.com/course/the-python-mega-course)具有相同的值。
  • 我无法查看教程的代码,因为它位于付费墙后面。但是,如果这是 100% 相同的代码,那么这是一个糟糕的教程,它使用了恰好与某些特定数据一起工作的错误代码。
  • 其实还不错,我就是不耐烦了,教程实际上帮我解决了后面视频中的大部分问题。我猜当时模块正在工作,现在它已被弃用。教程真的很有帮助,不想给我的老师带来麻烦。????

标签: python bokeh pandas-datareader


【解决方案1】:

我通过将数据源从 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)

希望对以后遇到同样困难的人有所帮助

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多