Grabbing data from entire index (e.g., DJIA) using pandas web.DataReader

user3314418 picture user3314418 · Apr 25, 2014 · Viewed 7.8k times · Source

I know how to get individual stocks. How might I get data for an entire index, like the DJI? https://www.google.com/finance?q=INDEXDJX%3A.DJI&ei=zsVZU4iADYKI6AGoXA

I'd like to analyze the stock market as a whole from as far back as possible

start, end = dt.datetime(1950, 1, 1), dt.datetime(2013, 12, 31)

data = web.DataReader('.DJI', 'yahoo', start, end)

Answer

Jacob Amos picture Jacob Amos · May 2, 2014

Google Finance and Yahoo Finance handle their symbols for indices differently. Google would denote the Dow as ".DJI" whereas in Yahoo it would be "^DJI".

For some reason when I run the code Pandas is having trouble finding data for the Dow from Yahoo, but it can find it for the S&P and the Nasdaq.

# this works
web.DataReader('^GSPC','yahoo')  # S&P 500
web.DataReader('^IXIC','yahoo')  # NASDAQ

# this doesn't
web.DataReader('^DJI','yahoo')   # Dow

If you specifically want Dow data, Pandas also lets you use FRED data, so you can get alternatively take that route, though it won't include all the price data, just the close prices.

web.DataReader('DJIA','fred')

Another possibility would be to use Quandl. They have tons of datasets (financial, economic, demographic, etc.) that might be useful for market analysis. While it still only gets the close prices and requires knowing their sometimes cryptic "codes", here is a sample:

import Quandl
dow_code = 'BCB/UDJIAD1'
Quandl.get(dow_code)

You may need to create a Quandl account (it's free) to get the authorization token that allows external mining into their database, but this is another possible workaround for you :)

Hope this helps.