{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandas Datareader\n",
"\n",
"** NOTE: Not every geographical location works well with pandas datareader, your firewall may also block it!**\n",
"\n",
"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:\n",
"\n",
"* Yahoo! Finance\n",
"* Google Finance\n",
"* Enigma\n",
"* St.Louis FED (FRED)\n",
"* Kenneth French’s data library\n",
"* World Bank\n",
"* OECD\n",
"* Eurostat\n",
"* Thrift Savings Plan\n",
"* Oanda currency historical rate\n",
"* Nasdaq Trader symbol definitions (remote_data.nasdaq_symbols)\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas_datareader.data as web\n",
"\n",
"import datetime\n",
"\n",
"start = datetime.datetime(2015, 1, 1)\n",
"\n",
"end = datetime.datetime(2017, 1, 1)\n",
"\n",
"facebook = web.DataReader(\"FB\", 'google', start, end)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Open | \n",
" High | \n",
" Low | \n",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-01-02 | \n",
" 78.58 | \n",
" 78.93 | \n",
" 77.70 | \n",
" 78.45 | \n",
" 18177475 | \n",
"
\n",
" \n",
" 2015-01-05 | \n",
" 77.98 | \n",
" 79.25 | \n",
" 76.86 | \n",
" 77.19 | \n",
" 26452191 | \n",
"
\n",
" \n",
" 2015-01-06 | \n",
" 77.23 | \n",
" 77.59 | \n",
" 75.36 | \n",
" 76.15 | \n",
" 27399288 | \n",
"
\n",
" \n",
" 2015-01-07 | \n",
" 76.76 | \n",
" 77.36 | \n",
" 75.82 | \n",
" 76.15 | \n",
" 22045333 | \n",
"
\n",
" \n",
" 2015-01-08 | \n",
" 76.74 | \n",
" 78.23 | \n",
" 76.08 | \n",
" 78.18 | \n",
" 23960953 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Open High Low Close Volume\n",
"Date \n",
"2015-01-02 78.58 78.93 77.70 78.45 18177475\n",
"2015-01-05 77.98 79.25 76.86 77.19 26452191\n",
"2015-01-06 77.23 77.59 75.36 76.15 27399288\n",
"2015-01-07 76.76 77.36 75.82 76.15 22045333\n",
"2015-01-08 76.74 78.23 76.08 78.18 23960953"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"facebook.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Experimental Options\n",
"\n",
"The Options class allows the download of options data from Google Finance.\n",
"\n",
"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.\n",
"\n",
"Available expiry dates can be accessed from the expiry_dates property."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pandas_datareader.data import Options\n",
"\n",
"fb_options = Options('FB', 'google')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"data = fb_options.get_options_data(expiry=fb_options.expiry_dates[0])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" | \n",
" | \n",
" Last | \n",
" Bid | \n",
" Ask | \n",
" Chg | \n",
" PctChg | \n",
" Vol | \n",
" Open_Int | \n",
" Root | \n",
" Underlying_Price | \n",
" Quote_Time | \n",
"
\n",
" \n",
" Strike | \n",
" Expiry | \n",
" Type | \n",
" Symbol | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 5.0 | \n",
" 2018-01-19 | \n",
" call | \n",
" FB180119C00005000 | \n",
" 149.10 | \n",
" 150.00 | \n",
" 150.65 | \n",
" 0.3 | \n",
" 0.20 | \n",
" 50.0 | \n",
" 9528.0 | \n",
" FB | \n",
" 155.27 | \n",
" 2017-07-11 23:35:24.466303 | \n",
"
\n",
" \n",
" put | \n",
" FB180119P00005000 | \n",
" 0.01 | \n",
" NaN | \n",
" 0.03 | \n",
" 0.0 | \n",
" 0.00 | \n",
" NaN | \n",
" 3053.0 | \n",
" FB | \n",
" 155.27 | \n",
" 2017-07-11 23:35:24.466303 | \n",
"
\n",
" \n",
" 10.0 | \n",
" 2018-01-19 | \n",
" call | \n",
" FB180119C00010000 | \n",
" 143.20 | \n",
" 145.05 | \n",
" 145.75 | \n",
" -0.5 | \n",
" -0.35 | \n",
" 40.0 | \n",
" 2855.0 | \n",
" FB | \n",
" 155.27 | \n",
" 2017-07-11 23:35:24.466303 | \n",
"
\n",
" \n",
" put | \n",
" FB180119P00010000 | \n",
" 0.01 | \n",
" NaN | \n",
" 0.02 | \n",
" 0.0 | \n",
" 0.00 | \n",
" NaN | \n",
" 2056.0 | \n",
" FB | \n",
" 155.27 | \n",
" 2017-07-11 23:35:24.466303 | \n",
"
\n",
" \n",
" 15.0 | \n",
" 2018-01-19 | \n",
" call | \n",
" FB180119C00015000 | \n",
" 135.80 | \n",
" 140.10 | \n",
" 140.80 | \n",
" 0.0 | \n",
" 0.00 | \n",
" NaN | \n",
" 240.0 | \n",
" FB | \n",
" 155.27 | \n",
" 2017-07-11 23:35:24.466303 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Last Bid Ask Chg PctChg \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 149.10 150.00 150.65 0.3 0.20 \n",
" put FB180119P00005000 0.01 NaN 0.03 0.0 0.00 \n",
"10.0 2018-01-19 call FB180119C00010000 143.20 145.05 145.75 -0.5 -0.35 \n",
" put FB180119P00010000 0.01 NaN 0.02 0.0 0.00 \n",
"15.0 2018-01-19 call FB180119C00015000 135.80 140.10 140.80 0.0 0.00 \n",
"\n",
" Vol Open_Int Root \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 50.0 9528.0 FB \n",
" put FB180119P00005000 NaN 3053.0 FB \n",
"10.0 2018-01-19 call FB180119C00010000 40.0 2855.0 FB \n",
" put FB180119P00010000 NaN 2056.0 FB \n",
"15.0 2018-01-19 call FB180119C00015000 NaN 240.0 FB \n",
"\n",
" Underlying_Price \\\n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 155.27 \n",
" put FB180119P00005000 155.27 \n",
"10.0 2018-01-19 call FB180119C00010000 155.27 \n",
" put FB180119P00010000 155.27 \n",
"15.0 2018-01-19 call FB180119C00015000 155.27 \n",
"\n",
" Quote_Time \n",
"Strike Expiry Type Symbol \n",
"5.0 2018-01-19 call FB180119C00005000 2017-07-11 23:35:24.466303 \n",
" put FB180119P00005000 2017-07-11 23:35:24.466303 \n",
"10.0 2018-01-19 call FB180119C00010000 2017-07-11 23:35:24.466303 \n",
" put FB180119P00010000 2017-07-11 23:35:24.466303 \n",
"15.0 2018-01-19 call FB180119C00015000 2017-07-11 23:35:24.466303 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FRED"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas_datareader.data as web\n",
"\n",
"import datetime\n",
"\n",
"start = datetime.datetime(2010, 1, 1)\n",
"\n",
"end = datetime.datetime(2017, 1, 1)\n",
"\n",
"gdp = web.DataReader(\"GDP\", \"fred\", start, end)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" GDP | \n",
"
\n",
" \n",
" DATE | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2010-01-01 | \n",
" 14681.1 | \n",
"
\n",
" \n",
" 2010-04-01 | \n",
" 14888.6 | \n",
"
\n",
" \n",
" 2010-07-01 | \n",
" 15057.7 | \n",
"
\n",
" \n",
" 2010-10-01 | \n",
" 15230.2 | \n",
"
\n",
" \n",
" 2011-01-01 | \n",
" 15238.4 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" GDP\n",
"DATE \n",
"2010-01-01 14681.1\n",
"2010-04-01 14888.6\n",
"2010-07-01 15057.7\n",
"2010-10-01 15230.2\n",
"2011-01-01 15238.4"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdp.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}