pandas “pandas_datareader”中的“get_data_yahoo”返回空数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45704033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:15:15  来源:igfitidea点击:

"get_data_yahoo" from "pandas_datareader" returns empty DataFrame

pythonpandasyahoo-financepandas-datareader

提问by codeedoc

I was trying to fetch stock data using the "get_data_yahoo" method from "pandas_datareader" so I wrote the following to test it out. I learned that there have been problems with the Yahoo API so I followed the instruction hereto add the "fix_yahoo_finance" module and yf.pdr_override().

我试图使用“pandas_datareader”中的“get_data_yahoo”方法获取股票数据,所以我写了以下内容来测试它。我了解到 Yahoo API 存在问题,因此我按照此处的说明添加了“fix_yahoo_finance”模块和yf.pdr_override().

from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
from datetime import datetime

yf.pdr_override()
a = pdr.get_data_yahoo('AAPL', start=datetime(2017, 8, 13), end=datetime(2017, 8, 14))
b = pdr.get_data_yahoo('AMZN', start=datetime(2017, 8, 13), end=datetime(2017, 8, 14))
c = pdr.get_data_yahoo('MSFT', start=datetime(2017, 8, 13), end=datetime(2017, 8, 14))
print(a)
print(b)
print(c)

However, when I ran the above code, sometimes the stocks could not be fetched and that resulted in one or two of the DataFrame (or all three) being empyty as shown below. enter image description hereenter image description hereIn the first picture only the first one is fetched while in the second picture only the second is fetched.
I tried out different tickers and ran many times and this seems to be a random pattern. Does anyone know what's going on? Is it the module broken again or something I can fix on my end? Thanks in advance.

但是,当我运行上面的代码时,有时无法获取股票,这导致一两个 DataFrame(或全部三个)为空,如下所示。 在第一张图片中只获取第一个,而在第二个图片中只获取第二个。 我尝试了不同的代码并运行了很多次,这似乎是一种随机模式。有谁知道发生了什么?是模块再次损坏还是我可以修复的东西?提前致谢。在此处输入图片说明在此处输入图片说明

回答by Will Ru

I can't diagnose the exact issue happening here right now but here is a workaround:

我现在无法诊断这里发生的确切问题,但这里有一个解决方法:

stock_list = ['AMZN', 'MSFT', 'AAPL']
stock_dict = {}
for stock in stock_list:
    dim = (0,0)
    while dim != (1,6):
       s = pdr.get_data_yahoo(stock, start="2017-08-13",end="2017-08-14")
       dim = s.shape
    stock_dict[stock] = s