{
"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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-01-02 | \n",
" 38.0061 | \n",
" 6906098 | \n",
"
\n",
" \n",
" 2015-01-05 | \n",
" 37.2781 | \n",
" 11623796 | \n",
"
\n",
" \n",
" 2015-01-06 | \n",
" 36.9748 | \n",
" 7664340 | \n",
"
\n",
" \n",
" 2015-01-07 | \n",
" 37.8848 | \n",
" 9732554 | \n",
"
\n",
" \n",
" 2015-01-08 | \n",
" 38.4961 | \n",
" 13170548 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-12-24 | \n",
" 60.56 | \n",
" 6323252 | \n",
"
\n",
" \n",
" 2018-12-26 | \n",
" 63.08 | \n",
" 16646238 | \n",
"
\n",
" \n",
" 2018-12-27 | \n",
" 63.20 | \n",
" 11308081 | \n",
"
\n",
" \n",
" 2018-12-28 | \n",
" 63.39 | \n",
" 7712127 | \n",
"
\n",
" \n",
" 2018-12-31 | \n",
" 64.40 | \n",
" 7690183 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-01-02 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2015-01-05 | \n",
" 38.0061 | \n",
" 6906098.0 | \n",
"
\n",
" \n",
" 2015-01-06 | \n",
" 37.2781 | \n",
" 11623796.0 | \n",
"
\n",
" \n",
" 2015-01-07 | \n",
" 36.9748 | \n",
" 7664340.0 | \n",
"
\n",
" \n",
" 2015-01-08 | \n",
" 37.8848 | \n",
" 9732554.0 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-12-24 | \n",
" 61.39 | \n",
" 23524888.0 | \n",
"
\n",
" \n",
" 2018-12-26 | \n",
" 60.56 | \n",
" 6323252.0 | \n",
"
\n",
" \n",
" 2018-12-27 | \n",
" 63.08 | \n",
" 16646238.0 | \n",
"
\n",
" \n",
" 2018-12-28 | \n",
" 63.20 | \n",
" 11308081.0 | \n",
"
\n",
" \n",
" 2018-12-31 | \n",
" 63.39 | \n",
" 7712127.0 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-01-02 | \n",
" 37.2781 | \n",
" 11623796.0 | \n",
"
\n",
" \n",
" 2015-01-05 | \n",
" 36.9748 | \n",
" 7664340.0 | \n",
"
\n",
" \n",
" 2015-01-06 | \n",
" 37.8848 | \n",
" 9732554.0 | \n",
"
\n",
" \n",
" 2015-01-07 | \n",
" 38.4961 | \n",
" 13170548.0 | \n",
"
\n",
" \n",
" 2015-01-08 | \n",
" 37.2361 | \n",
" 27556706.0 | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-12-24 | \n",
" 63.08 | \n",
" 16646238.0 | \n",
"
\n",
" \n",
" 2018-12-26 | \n",
" 63.20 | \n",
" 11308081.0 | \n",
"
\n",
" \n",
" 2018-12-27 | \n",
" 63.39 | \n",
" 7712127.0 | \n",
"
\n",
" \n",
" 2018-12-28 | \n",
" 64.40 | \n",
" 7690183.0 | \n",
"
\n",
" \n",
" 2018-12-31 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\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",
" Close | \n",
" Volume | \n",
"
\n",
" \n",
" Date | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-01-31 | \n",
" 38.0061 | \n",
" 6906098 | \n",
"
\n",
" \n",
" 2015-01-31 | \n",
" 37.2781 | \n",
" 11623796 | \n",
"
\n",
" \n",
" 2015-01-31 | \n",
" 36.9748 | \n",
" 7664340 | \n",
"
\n",
" \n",
" 2015-01-31 | \n",
" 37.8848 | \n",
" 9732554 | \n",
"
\n",
" \n",
" 2015-01-31 | \n",
" 38.4961 | \n",
" 13170548 | \n",
"
\n",
" \n",
"
\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
}