Pandas Datareader

NOTE: Not every geographical location works well with pandas datareader, your firewall may also block it!

Functions from pandas_datareader.data and pandas_datareader.wb extract data from various Internet sources into a pandas DataFrame. Currently the following sources are supported:

  • Google Finance
  • Enigma
  • St.Louis FED (FRED)
  • Kenneth French’s data library
  • World Bank
  • OECD
  • Eurostat
  • Thrift Savings Plan
  • Oanda currency historical rate
  • Nasdaq Trader symbol definitions (remote_data.nasdaq_symbols)

It should be noted, that various sources support different kinds of data, so not all sources implement the same methods and the data elements returned might also differ. Visit the <a href="https://pandas-datareader.readthedocs.io/en/latest/remote_data.html" target = "_blank">pandas-datareader site</a> for more details.

Note on Using Pandas-DataReader in Anaconda

If you encounter a module not found error when you try to use the pandas_datareader module. This is because Pandas Datareader Module is not part of Anaconda Dist.

To inatall Pandas Datareader

At Anaconda prompt:

conda install -c https://conda.anaconda.org/anaconda pandas-datareader

Alternatively, at command prompt

pip install pandas-datareader

Imports

In [ ]:
import pandas_datareader.data as web
import datetime

Read Data

  • The closing price "Close" from Google is the adjusted closing price
In [3]:
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2017, 1, 1)

facebook = web.DataReader("FB", 'google', start, end)
In [4]:
facebook.head()
Out[4]:
Open High Low Close Volume
Date
2015-01-02 78.58 78.93 77.70 78.45 18177475
2015-01-05 77.98 79.25 76.86 77.19 26452191
2015-01-06 77.23 77.59 75.36 76.15 27399288
2015-01-07 76.76 77.36 75.82 76.15 22045333
2015-01-08 76.74 78.23 76.08 78.18 23960953

Experimental Options

The Options class allows the download of options data from Google Finance.

In [5]:
from pandas_datareader.data import Options

fb_options = Options('FB', 'google')

The get_options_data method downloads options data for specified expiry date and provides a formatted DataFrame with a hierarchical index, so its easy to get to the specific option you want.

Available expiry dates can be accessed from the expiry_dates property.

In [8]:
data = fb_options.get_options_data(expiry=fb_options.expiry_dates[0])
In [10]:
data.head()
Out[10]:
Last Bid Ask Chg PctChg Vol Open_Int Root Underlying_Price Quote_Time
Strike Expiry Type Symbol
5.0 2018-01-19 call FB180119C00005000 149.10 150.00 150.65 0.3 0.20 50.0 9528.0 FB 155.27 2017-07-11 23:35:24.466303
put FB180119P00005000 0.01 NaN 0.03 0.0 0.00 NaN 3053.0 FB 155.27 2017-07-11 23:35:24.466303
10.0 2018-01-19 call FB180119C00010000 143.20 145.05 145.75 -0.5 -0.35 40.0 2855.0 FB 155.27 2017-07-11 23:35:24.466303
put FB180119P00010000 0.01 NaN 0.02 0.0 0.00 NaN 2056.0 FB 155.27 2017-07-11 23:35:24.466303
15.0 2018-01-19 call FB180119C00015000 135.80 140.10 140.80 0.0 0.00 NaN 240.0 FB 155.27 2017-07-11 23:35:24.466303

FRED

In [11]:
import pandas_datareader.data as web
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 1, 1)

gdp = web.DataReader("GDP", "fred", start, end)
In [12]:
gdp.head()
Out[12]:
GDP
DATE
2010-01-01 14681.1
2010-04-01 14888.6
2010-07-01 15057.7
2010-10-01 15230.2
2011-01-01 15238.4

FRED - Multiple series

In [1]:
import pandas_datareader.data as web
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2010, 5, 1)

inflation = web.DataReader(['CPIAUCSL', 'CPILFESL'], 'fred', start, end)
In [2]:
inflation.head()
Out[2]:
CPIAUCSL CPILFESL
DATE
2010-01-01 217.488 220.633
2010-02-01 217.281 220.731
2010-03-01 217.353 220.783
2010-04-01 217.403 220.822
2010-05-01 217.290 220.962