|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<h1 align=\"center\"> Part 2: Time Series Data Nonlinear Regression, Shifting Dataframes, Daily Growth,</h1>" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "This code demonstrates how to view time series data in pandas as well as nonlinear regression, shifting dataframe, apply by month, \n", |
| 15 | + "\n", |
| 16 | + "**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in.**" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "<b> Part 1 </b>: https://www.youtube.com/watch?v=OwnaUVt6VVE" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "<h3 align='Left'> Importing Libraries</h3>" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 26, |
| 36 | + "metadata": { |
| 37 | + "collapsed": false |
| 38 | + }, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "import pandas as pd\n", |
| 42 | + "import pandas_datareader.data as web\n", |
| 43 | + "import numpy as np\n", |
| 44 | + "import matplotlib.pyplot as plt\n", |
| 45 | + "from sklearn.linear_model import LinearRegression\n", |
| 46 | + "%matplotlib inline" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "<h3 align='Left'> Getting Data and Viewing with Pandas </h3>" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 40, |
| 59 | + "metadata": { |
| 60 | + "collapsed": false |
| 61 | + }, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "data": { |
| 65 | + "text/html": [ |
| 66 | + "<div>\n", |
| 67 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 68 | + " <thead>\n", |
| 69 | + " <tr style=\"text-align: right;\">\n", |
| 70 | + " <th></th>\n", |
| 71 | + " <th>Open</th>\n", |
| 72 | + " <th>High</th>\n", |
| 73 | + " <th>Low</th>\n", |
| 74 | + " <th>Close</th>\n", |
| 75 | + " </tr>\n", |
| 76 | + " <tr>\n", |
| 77 | + " <th>Date</th>\n", |
| 78 | + " <th></th>\n", |
| 79 | + " <th></th>\n", |
| 80 | + " <th></th>\n", |
| 81 | + " <th></th>\n", |
| 82 | + " </tr>\n", |
| 83 | + " </thead>\n", |
| 84 | + " <tbody>\n", |
| 85 | + " <tr>\n", |
| 86 | + " <th>2009-03-16</th>\n", |
| 87 | + " <td>162.83</td>\n", |
| 88 | + " <td>164.70</td>\n", |
| 89 | + " <td>159.14</td>\n", |
| 90 | + " <td>159.69</td>\n", |
| 91 | + " </tr>\n", |
| 92 | + " <tr>\n", |
| 93 | + " <th>2009-03-17</th>\n", |
| 94 | + " <td>159.93</td>\n", |
| 95 | + " <td>167.50</td>\n", |
| 96 | + " <td>159.39</td>\n", |
| 97 | + " <td>167.50</td>\n", |
| 98 | + " </tr>\n", |
| 99 | + " <tr>\n", |
| 100 | + " <th>2009-03-18</th>\n", |
| 101 | + " <td>167.24</td>\n", |
| 102 | + " <td>169.83</td>\n", |
| 103 | + " <td>163.86</td>\n", |
| 104 | + " <td>166.38</td>\n", |
| 105 | + " </tr>\n", |
| 106 | + " <tr>\n", |
| 107 | + " <th>2009-03-19</th>\n", |
| 108 | + " <td>165.67</td>\n", |
| 109 | + " <td>167.83</td>\n", |
| 110 | + " <td>163.53</td>\n", |
| 111 | + " <td>164.81</td>\n", |
| 112 | + " </tr>\n", |
| 113 | + " <tr>\n", |
| 114 | + " <th>2009-03-20</th>\n", |
| 115 | + " <td>164.98</td>\n", |
| 116 | + " <td>166.33</td>\n", |
| 117 | + " <td>163.01</td>\n", |
| 118 | + " <td>164.91</td>\n", |
| 119 | + " </tr>\n", |
| 120 | + " </tbody>\n", |
| 121 | + "</table>\n", |
| 122 | + "</div>" |
| 123 | + ], |
| 124 | + "text/plain": [ |
| 125 | + " Open High Low Close\n", |
| 126 | + "Date \n", |
| 127 | + "2009-03-16 162.83 164.70 159.14 159.69\n", |
| 128 | + "2009-03-17 159.93 167.50 159.39 167.50\n", |
| 129 | + "2009-03-18 167.24 169.83 163.86 166.38\n", |
| 130 | + "2009-03-19 165.67 167.83 163.53 164.81\n", |
| 131 | + "2009-03-20 164.98 166.33 163.01 164.91" |
| 132 | + ] |
| 133 | + }, |
| 134 | + "execution_count": 40, |
| 135 | + "metadata": {}, |
| 136 | + "output_type": "execute_result" |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "# https://pandas-datareader.readthedocs.io/en/latest/remote_data.html\n", |
| 141 | + "google = web.DataReader('GOOG', data_source = 'google', start = '3/14/2009', end = '4/14/2016')\n", |
| 142 | + "google = google.drop('Volume', axis = 1 )\n", |
| 143 | + "google.head()" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "<h3 align='Left'> Calculate Daily Price Variation </h3>" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "metadata": {}, |
| 156 | + "source": [ |
| 157 | + "shift in pandas\n", |
| 158 | + "http://www.productiveegg.com/productive-egg-blog/how-python-pandas-makes.html" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "markdown", |
| 163 | + "metadata": {}, |
| 164 | + "source": [ |
| 165 | + "daily price variation:\n", |
| 166 | + "http://finance.zacks.com/calculate-daily-price-variation-stocks-8299.html" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 52, |
| 172 | + "metadata": { |
| 173 | + "collapsed": false |
| 174 | + }, |
| 175 | + "outputs": [ |
| 176 | + { |
| 177 | + "data": { |
| 178 | + "text/html": [ |
| 179 | + "<div>\n", |
| 180 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 181 | + " <thead>\n", |
| 182 | + " <tr style=\"text-align: right;\">\n", |
| 183 | + " <th></th>\n", |
| 184 | + " <th>Open</th>\n", |
| 185 | + " </tr>\n", |
| 186 | + " <tr>\n", |
| 187 | + " <th>Date</th>\n", |
| 188 | + " <th></th>\n", |
| 189 | + " </tr>\n", |
| 190 | + " </thead>\n", |
| 191 | + " <tbody>\n", |
| 192 | + " <tr>\n", |
| 193 | + " <th>2009-03-16</th>\n", |
| 194 | + " <td>-2.90</td>\n", |
| 195 | + " </tr>\n", |
| 196 | + " <tr>\n", |
| 197 | + " <th>2009-03-17</th>\n", |
| 198 | + " <td>7.31</td>\n", |
| 199 | + " </tr>\n", |
| 200 | + " <tr>\n", |
| 201 | + " <th>2009-03-18</th>\n", |
| 202 | + " <td>-1.57</td>\n", |
| 203 | + " </tr>\n", |
| 204 | + " <tr>\n", |
| 205 | + " <th>2009-03-19</th>\n", |
| 206 | + " <td>-0.69</td>\n", |
| 207 | + " </tr>\n", |
| 208 | + " <tr>\n", |
| 209 | + " <th>2009-03-20</th>\n", |
| 210 | + " <td>1.63</td>\n", |
| 211 | + " </tr>\n", |
| 212 | + " </tbody>\n", |
| 213 | + "</table>\n", |
| 214 | + "</div>" |
| 215 | + ], |
| 216 | + "text/plain": [ |
| 217 | + " Open\n", |
| 218 | + "Date \n", |
| 219 | + "2009-03-16 -2.90\n", |
| 220 | + "2009-03-17 7.31\n", |
| 221 | + "2009-03-18 -1.57\n", |
| 222 | + "2009-03-19 -0.69\n", |
| 223 | + "2009-03-20 1.63" |
| 224 | + ] |
| 225 | + }, |
| 226 | + "execution_count": 52, |
| 227 | + "metadata": {}, |
| 228 | + "output_type": "execute_result" |
| 229 | + } |
| 230 | + ], |
| 231 | + "source": [ |
| 232 | + "daily_shift = (google['Open'].shift(-1) - google['Open'])\n", |
| 233 | + "pd.DataFrame(data = daily_shift.head())" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": 53, |
| 239 | + "metadata": { |
| 240 | + "collapsed": false |
| 241 | + }, |
| 242 | + "outputs": [ |
| 243 | + { |
| 244 | + "data": { |
| 245 | + "text/html": [ |
| 246 | + "<div>\n", |
| 247 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 248 | + " <thead>\n", |
| 249 | + " <tr style=\"text-align: right;\">\n", |
| 250 | + " <th></th>\n", |
| 251 | + " <th>0</th>\n", |
| 252 | + " </tr>\n", |
| 253 | + " <tr>\n", |
| 254 | + " <th>Date</th>\n", |
| 255 | + " <th></th>\n", |
| 256 | + " </tr>\n", |
| 257 | + " </thead>\n", |
| 258 | + " <tbody>\n", |
| 259 | + " <tr>\n", |
| 260 | + " <th>2009-03-16</th>\n", |
| 261 | + " <td>5.56</td>\n", |
| 262 | + " </tr>\n", |
| 263 | + " <tr>\n", |
| 264 | + " <th>2009-03-17</th>\n", |
| 265 | + " <td>8.11</td>\n", |
| 266 | + " </tr>\n", |
| 267 | + " <tr>\n", |
| 268 | + " <th>2009-03-18</th>\n", |
| 269 | + " <td>5.97</td>\n", |
| 270 | + " </tr>\n", |
| 271 | + " <tr>\n", |
| 272 | + " <th>2009-03-19</th>\n", |
| 273 | + " <td>4.30</td>\n", |
| 274 | + " </tr>\n", |
| 275 | + " <tr>\n", |
| 276 | + " <th>2009-03-20</th>\n", |
| 277 | + " <td>3.32</td>\n", |
| 278 | + " </tr>\n", |
| 279 | + " </tbody>\n", |
| 280 | + "</table>\n", |
| 281 | + "</div>" |
| 282 | + ], |
| 283 | + "text/plain": [ |
| 284 | + " 0\n", |
| 285 | + "Date \n", |
| 286 | + "2009-03-16 5.56\n", |
| 287 | + "2009-03-17 8.11\n", |
| 288 | + "2009-03-18 5.97\n", |
| 289 | + "2009-03-19 4.30\n", |
| 290 | + "2009-03-20 3.32" |
| 291 | + ] |
| 292 | + }, |
| 293 | + "execution_count": 53, |
| 294 | + "metadata": {}, |
| 295 | + "output_type": "execute_result" |
| 296 | + } |
| 297 | + ], |
| 298 | + "source": [ |
| 299 | + "# Difference between two columns\n", |
| 300 | + "difference = google['High'] - google['Low']\n", |
| 301 | + "pd.DataFrame(data = difference.head())" |
| 302 | + ] |
| 303 | + }, |
| 304 | + { |
| 305 | + "cell_type": "markdown", |
| 306 | + "metadata": {}, |
| 307 | + "source": [ |
| 308 | + "<h3 align='Left'> Calculating Monthly Price Variation using a GroupBy </h3>" |
| 309 | + ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": 55, |
| 314 | + "metadata": { |
| 315 | + "collapsed": true |
| 316 | + }, |
| 317 | + "outputs": [], |
| 318 | + "source": [ |
| 319 | + "# First link is great others not so much\n", |
| 320 | + "# http://stackoverflow.com/questions/24082784/pandas-dataframe-groupby-datetime-month\n", |
| 321 | + "# http://stackoverflow.com/questions/30405413/python-pandas-extract-year-from-datetime-dfyear-dfdate-year-is-not" |
| 322 | + ] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "code", |
| 326 | + "execution_count": null, |
| 327 | + "metadata": { |
| 328 | + "collapsed": true |
| 329 | + }, |
| 330 | + "outputs": [], |
| 331 | + "source": [] |
| 332 | + } |
| 333 | + ], |
| 334 | + "metadata": { |
| 335 | + "kernelspec": { |
| 336 | + "display_name": "Python 2", |
| 337 | + "language": "python", |
| 338 | + "name": "python2" |
| 339 | + }, |
| 340 | + "language_info": { |
| 341 | + "codemirror_mode": { |
| 342 | + "name": "ipython", |
| 343 | + "version": 2 |
| 344 | + }, |
| 345 | + "file_extension": ".py", |
| 346 | + "mimetype": "text/x-python", |
| 347 | + "name": "python", |
| 348 | + "nbconvert_exporter": "python", |
| 349 | + "pygments_lexer": "ipython2", |
| 350 | + "version": "2.7.12" |
| 351 | + } |
| 352 | + }, |
| 353 | + "nbformat": 4, |
| 354 | + "nbformat_minor": 0 |
| 355 | +} |
0 commit comments