Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading data from Yahoo! Finance with pandas

Tags:

python

pandas

I am working my way through Wes McKinney's book Python For Data Analysis and on page 139 under Correlation and Covariance, I am getting an error when I try to run his code to obtain data from Yahoo! Finance.

Here is what I am running:

#CORRELATION AND COVARIANCE
import pandas.io.data as web

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')

price = DataFrame({tic: data['Adj Close']
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})

Here is the error I am getting:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 390, in get_data_yahoo
    adjust_price, ret_index, chunksize, 'yahoo', name)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 336, in _get_data_from
    hist_data = src_fn(symbols, start, end, retry_count, pause)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 190, in _get_hist_yahoo
    return _retry_read_url(url, retry_count, pause, 'Yahoo!')
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 169, in _retry_read_url
    "return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=1&f=2010&g=d&ignore=.csv'
>>> ... >>> >>> ... >>> 

Any idea on what the problem is?

like image 734
statsNoob Avatar asked May 13 '14 21:05

statsNoob


People also ask

Can you scrape data from Yahoo Finance?

You can scrape Yahoo Finance web source for currencies; you just have to identify the tags with the needed information. As there are no certain attributes in the HTML code of the Yahoo Finance page, we retrieved the data using a data ID.


2 Answers

As Karl pointed out, the ticker had changed meaning Yahoo returns a 'page not found'.

When polling data from the web, it is a good idea to wrap the call in a try except

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    try:
        all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')
        price = DataFrame({tic: data['Adj Close']
                    for tic, data in all_data.iteritems()})
        volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})
    except:
        print "Cant find ", ticker
like image 158
acutesoftware Avatar answered Sep 23 '22 09:09

acutesoftware


Had the same problem and changing 'GOOG' to 'GOOGL' seems to work, once you've followed these instructions to switch from pandas.io.data to pandas_datareader.data.

http://pandas-datareader.readthedocs.org/en/latest/remote_data.html#yahoo-finance

like image 43
andrewpederson Avatar answered Sep 25 '22 09:09

andrewpederson