{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseVolume
Date
2015-01-0278.5878.9377.7078.4518177475
2015-01-0577.9879.2576.8677.1926452191
2015-01-0677.2377.5975.3676.1527399288
2015-01-0776.7677.3675.8276.1522045333
2015-01-0876.7478.2376.0878.1823960953
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LastBidAskChgPctChgVolOpen_IntRootUnderlying_PriceQuote_Time
StrikeExpiryTypeSymbol
5.02018-01-19callFB180119C00005000149.10150.00150.650.30.2050.09528.0FB155.272017-07-11 23:35:24.466303
putFB180119P000050000.01NaN0.030.00.00NaN3053.0FB155.272017-07-11 23:35:24.466303
10.02018-01-19callFB180119C00010000143.20145.05145.75-0.5-0.3540.02855.0FB155.272017-07-11 23:35:24.466303
putFB180119P000100000.01NaN0.020.00.00NaN2056.0FB155.272017-07-11 23:35:24.466303
15.02018-01-19callFB180119C00015000135.80140.10140.800.00.00NaN240.0FB155.272017-07-11 23:35:24.466303
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GDP
DATE
2010-01-0114681.1
2010-04-0114888.6
2010-07-0115057.7
2010-10-0115230.2
2011-01-0115238.4
\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 }