{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Copyright Pierian Data
\n", "
For more information, visit us at www.pieriandata.com
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Time Shifting\n", "\n", "Sometimes you may need to shift all your data up or down along the time series index. In fact, a lot of pandas built-in methods do this under the hood. This isn't something we'll do often in the course, but it's definitely good to know about this anyways!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.read_csv('../Data/starbucks.csv',index_col='Date',parse_dates=True)" ] }, { "cell_type": "code", "execution_count": 3, "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", "
CloseVolume
Date
2015-01-0238.00616906098
2015-01-0537.278111623796
2015-01-0636.97487664340
2015-01-0737.88489732554
2015-01-0838.496113170548
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2015-01-02 38.0061 6906098\n", "2015-01-05 37.2781 11623796\n", "2015-01-06 36.9748 7664340\n", "2015-01-07 37.8848 9732554\n", "2015-01-08 38.4961 13170548" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "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", "
CloseVolume
Date
2018-12-2460.566323252
2018-12-2663.0816646238
2018-12-2763.2011308081
2018-12-2863.397712127
2018-12-3164.407690183
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2018-12-24 60.56 6323252\n", "2018-12-26 63.08 16646238\n", "2018-12-27 63.20 11308081\n", "2018-12-28 63.39 7712127\n", "2018-12-31 64.40 7690183" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## .shift() forward\n", "This method shifts the entire date index a given number of rows, without regard for time periods (months & years).
It returns a modified copy of the original DataFrame." ] }, { "cell_type": "code", "execution_count": 5, "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", "
CloseVolume
Date
2015-01-02NaNNaN
2015-01-0538.00616906098.0
2015-01-0637.278111623796.0
2015-01-0736.97487664340.0
2015-01-0837.88489732554.0
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2015-01-02 NaN NaN\n", "2015-01-05 38.0061 6906098.0\n", "2015-01-06 37.2781 11623796.0\n", "2015-01-07 36.9748 7664340.0\n", "2015-01-08 37.8848 9732554.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shift(1).head()" ] }, { "cell_type": "code", "execution_count": 6, "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", "
CloseVolume
Date
2018-12-2461.3923524888.0
2018-12-2660.566323252.0
2018-12-2763.0816646238.0
2018-12-2863.2011308081.0
2018-12-3163.397712127.0
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2018-12-24 61.39 23524888.0\n", "2018-12-26 60.56 6323252.0\n", "2018-12-27 63.08 16646238.0\n", "2018-12-28 63.20 11308081.0\n", "2018-12-31 63.39 7712127.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# NOTE: You will lose that last piece of data that no longer has an index!\n", "df.shift(1).tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## .shift() backwards" ] }, { "cell_type": "code", "execution_count": 7, "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", "
CloseVolume
Date
2015-01-0237.278111623796.0
2015-01-0536.97487664340.0
2015-01-0637.88489732554.0
2015-01-0738.496113170548.0
2015-01-0837.236127556706.0
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2015-01-02 37.2781 11623796.0\n", "2015-01-05 36.9748 7664340.0\n", "2015-01-06 37.8848 9732554.0\n", "2015-01-07 38.4961 13170548.0\n", "2015-01-08 37.2361 27556706.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shift(-1).head()" ] }, { "cell_type": "code", "execution_count": 8, "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", "
CloseVolume
Date
2018-12-2463.0816646238.0
2018-12-2663.2011308081.0
2018-12-2763.397712127.0
2018-12-2864.407690183.0
2018-12-31NaNNaN
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2018-12-24 63.08 16646238.0\n", "2018-12-26 63.20 11308081.0\n", "2018-12-27 63.39 7712127.0\n", "2018-12-28 64.40 7690183.0\n", "2018-12-31 NaN NaN" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shift(-1).tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shifting based on Time Series Frequency Code\n", "\n", "We can choose to shift index values up or down without realigning the data by passing in a freq argument.
\n", "This method shifts dates to the next period based on a frequency code. Common codes are 'M' for month-end and 'A' for year-end.
Refer to the Time Series Offset Aliases table from the Time Resampling lecture for a full list of values, or click here.
" ] }, { "cell_type": "code", "execution_count": 9, "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", "
CloseVolume
Date
2015-01-3138.00616906098
2015-01-3137.278111623796
2015-01-3136.97487664340
2015-01-3137.88489732554
2015-01-3138.496113170548
\n", "
" ], "text/plain": [ " Close Volume\n", "Date \n", "2015-01-31 38.0061 6906098\n", "2015-01-31 37.2781 11623796\n", "2015-01-31 36.9748 7664340\n", "2015-01-31 37.8848 9732554\n", "2015-01-31 38.4961 13170548" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Shift everything forward one month\n", "df.shift(periods=1, freq='M').head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more info on time shifting visit http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.shift.html
\n", "Up next we'll look at rolling and expanding!" ] } ], "metadata": { "anaconda-cloud": {}, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }