codeedoc
codeedoc

Reputation: 494

"get_data_yahoo" from "pandas_datareader" returns empty DataFrame

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 here to add the "fix_yahoo_finance" module and 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 here enter image description here In 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.

Upvotes: 2

Views: 23489

Answers (2)

Aytaç Oral
Aytaç Oral

Reputation: 11

Try adding

stocks = [stock + '.AX' for stock in stockList]*

before calling

pdr.get_data_yahoo(**stocks**, start="2017-08-13",end="2017-08-14")

Upvotes: 1

Will Ru
Will Ru

Reputation: 131

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

Upvotes: 2

Related Questions