{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Financial Data - Google Finance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Introduction:\n", "\n", "This time you will get data from a website.\n", "\n", "\n", "### Step 1. Import the necessary libraries" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# package to extract data from various Internet sources into a DataFrame\n", "# make sure you have it installed\n", "from pandas_datareader import data, wb\n", "\n", "# package for dates\n", "import datetime as dt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 2. Create your time range (start and end variables). The start date should be 01/01/2015 and the end should today (whatever your today is)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2015, 1, 1, 0, 0)" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 3. Select the Apple, Tesla, Twitter, IBM, LinkedIn stocks symbols and assign them to a variable called stocks" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['AAPL', 'TSLA', 'IBM', 'LNKD']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 4. Read the data from google, assign to df and print it" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "Dimensions: 5 (items) x 399 (major_axis) x 4 (minor_axis)\n", "Items axis: Open to Volume\n", "Major_axis axis: 2015-01-02 00:00:00 to 2016-08-02 00:00:00\n", "Minor_axis axis: AAPL to TSLA" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 5. What is the type of structure of df ?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 'pandas.core.panel.Panel'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 6. Print all the Items axis values\n", "#### To learn more about the Panel structure go to [documentation](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#panel) " ] }, { "cell_type": "code", "execution_count": 120, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Index([u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 7. Good, now we know the data avaiable. Create a dataFrame called vol, with the Volume values." ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "collapsed": false }, "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", "
AAPLIBMLNKDTSLA
Date
2015-01-0253204626.05525466.01203743.04764443.0
2015-01-0564285491.04880389.01400562.05368477.0
2015-01-0665797116.06146712.02006546.06261936.0
2015-01-0740105934.04701839.0985016.02968390.0
2015-01-0859364547.04241113.01293955.03442509.0
\n", "
" ], "text/plain": [ " AAPL IBM LNKD TSLA\n", "Date \n", "2015-01-02 53204626.0 5525466.0 1203743.0 4764443.0\n", "2015-01-05 64285491.0 4880389.0 1400562.0 5368477.0\n", "2015-01-06 65797116.0 6146712.0 2006546.0 6261936.0\n", "2015-01-07 40105934.0 4701839.0 985016.0 2968390.0\n", "2015-01-08 59364547.0 4241113.0 1293955.0 3442509.0" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 8. Aggregate the data of Volume to weekly\n", "#### Hint: Be careful to not sum data from the same week of 2015 and other years." ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "collapsed": false, "scrolled": true }, "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", "
AAPLIBMLNKDTSLA
weekyear
1201553204626.05525466.01203743.04764443.0
2016343422014.025233098.06630485.020967926.0
22015283252615.024458400.07203125.022709607.0
2016302072797.029379214.09160521.022997290.0
32015304226647.023263206.07084168.030799137.0
\n", "
" ], "text/plain": [ " AAPL IBM LNKD TSLA\n", "week year \n", "1 2015 53204626.0 5525466.0 1203743.0 4764443.0\n", " 2016 343422014.0 25233098.0 6630485.0 20967926.0\n", "2 2015 283252615.0 24458400.0 7203125.0 22709607.0\n", " 2016 302072797.0 29379214.0 9160521.0 22997290.0\n", "3 2015 304226647.0 23263206.0 7084168.0 30799137.0" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 9. Find all the volume traded in the year of 2015" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "collapsed": false }, "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", "
AAPLIBMLNKDTSLA
year
20151.301994e+101.100959e+09440376163.01.085839e+09
20166.081474e+096.585250e+08453233878.07.540962e+08
\n", "
" ], "text/plain": [ " AAPL IBM LNKD TSLA\n", "year \n", "2015 1.301994e+10 1.100959e+09 440376163.0 1.085839e+09\n", "2016 6.081474e+09 6.585250e+08 453233878.0 7.540962e+08" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### BONUS: Create your own question and answer it." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }