Yahoo Finance API / URL not working: Python fix for Pandas DataReader

15,184

Solution 1

I have followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir and also upgraded pandas_datareader to be sure.

The "fix-yahoo-finance 0.0.6" worked well, for example BHP.AX:

from pandas_datareader import data as pdr
import fix_yahoo_finance

data = pdr.get_data_yahoo('BHP.AX', start='2017-04-23', end='2017-05-24')

Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. not the previous format. For my purpose, they are reset to the original format:

cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
data.reindex(columns=cols)

Solution 2

import pandas_datareader.data as pdweb
from pandas_datareader import data as pdr
import fix_yahoo_finance # must pip install first 
data = pdr.get_data_yahoo('SPY','2017-05-20','2017-05-23')
data = pdr.get_data_yahoo(['SPY','QQQ'],'2017-05-01','2017-05-23', as_panel=False,group_by = 'ticker')

Solution 3

I would recommend using Quandl. I'm not sure if Yahoo is going to be reliable after their acquisition. In Quandl if you have multiple symbols you have to loop. Read the docs and do something like this:

    import quandl as qdl
    start_date = '2016-01-01'
    end_date = '2017-05-22'
    for symbol in symbols:

        quandldata = qdl.get_table("WIKI/PRICES",qopts={"columns":["date", "adj_close"]},
        ticker=symbol, date = {'gte': start_date,'lte' : end_date})
        # specify that the quandldata df has index col = 'date'
        quandldata = quandldata.set_index(["date"], drop=True)
        # rename col adj close to the respective symbol to prevent clash w/ same name for all cols
        quandldata = quandldata.rename(columns={'adj_close': symbol})
        df = df.join(quandldata) 
Share:
15,184
artDeco
Author by

artDeco

Interests: Quant. Data. Code. Music. Film. Design. Architecture.

Updated on August 09, 2022

Comments

  • artDeco
    artDeco almost 2 years

    Yahoo Finance URL has not been accessible using the Pandas DataReader's "yahoo" method since 16 May 2017. I have yet to test this fix-yahoo-finance: https://pypi.python.org/pypi/fix-yahoo-finance that was posted just yesterday, with the statement: "Yahoo! finance has decommissioned their historical data API".

    EDIT August 2, 2017: I have since followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir, upgraded pandas_datareader to work with "fix-yahoo-finance 0.0.6", and amended codes:

    from pandas_datareader import data as pdr
    import fix_yahoo_finance
    
    data = pdr.get_data_yahoo('AAPL', start='2017-04-23', end='2017-05-24')
    

    Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. not the previous format. For my purpose, they are simply reset to the original format:

    cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
    data.reindex(columns=cols)
    
  • artDeco
    artDeco about 7 years
    Thanks and I'm not sure if Quandl provides data for ASX stocks. One data vendor eoddata.com may be worth a look.
  • MichaelRSF
    MichaelRSF about 7 years
    I can only grab 20 stocks with a time limit for every 10 minutes. If it exceeds more than 20 stocks within the 10 minute time limit, an error will pop up, breaking the code. So if I wanted to grab all 505 stocks from the S&P 500, it would take me a min of 4.2 hours (((505 stock/20 stocks)*10 minutes)/60 minutes). That's very inefficient. If you try to grab stock data for MSFT using pandas_datareader with yahoo, and click on the link that you get from the error saying "YAHOO! Will be right back." https://ichart.finance.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=‌​2016&d=0&e=1&f=2017&‌​g=d&ignore=.csv
  • Math Stout
    Math Stout about 7 years
    With Quandl, you'd want to open a free account. When you do, you'll get 2000calls/10min.
  • user1452494
    user1452494 over 6 years
    Google is not really a viable option because they adjust prices for splits, but not for dividends