You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 KiB

<html> <head> </head>

___

Copyright Pierian Data For more information, visit us at www.pieriandata.com

Time Shifting

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!

In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('../Data/starbucks.csv',index_col='Date',parse_dates=True)
In [3]:
df.head()
Out[3]:
Close Volume
Date
2015-01-02 38.0061 6906098
2015-01-05 37.2781 11623796
2015-01-06 36.9748 7664340
2015-01-07 37.8848 9732554
2015-01-08 38.4961 13170548
In [4]:
df.tail()
Out[4]:
Close Volume
Date
2018-12-24 60.56 6323252
2018-12-26 63.08 16646238
2018-12-27 63.20 11308081
2018-12-28 63.39 7712127
2018-12-31 64.40 7690183

.shift() forward

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.

In [5]:
df.shift(1).head()
Out[5]:
Close Volume
Date
2015-01-02 NaN NaN
2015-01-05 38.0061 6906098.0
2015-01-06 37.2781 11623796.0
2015-01-07 36.9748 7664340.0
2015-01-08 37.8848 9732554.0
In [6]:
# NOTE: You will lose that last piece of data that no longer has an index!
df.shift(1).tail()
Out[6]:
Close Volume
Date
2018-12-24 61.39 23524888.0
2018-12-26 60.56 6323252.0
2018-12-27 63.08 16646238.0
2018-12-28 63.20 11308081.0
2018-12-31 63.39 7712127.0

.shift() backwards

In [7]:
df.shift(-1).head()
Out[7]:
Close Volume
Date
2015-01-02 37.2781 11623796.0
2015-01-05 36.9748 7664340.0
2015-01-06 37.8848 9732554.0
2015-01-07 38.4961 13170548.0
2015-01-08 37.2361 27556706.0
In [8]:
df.shift(-1).tail()
Out[8]:
Close Volume
Date
2018-12-24 63.08 16646238.0
2018-12-26 63.20 11308081.0
2018-12-27 63.39 7712127.0
2018-12-28 64.40 7690183.0
2018-12-31 NaN NaN

Shifting based on Time Series Frequency Code

We can choose to shift index values up or down without realigning the data by passing in a freq argument.
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.

In [9]:
# Shift everything forward one month
df.shift(periods=1, freq='M').head()
Out[9]:
Close Volume
Date
2015-01-31 38.0061 6906098
2015-01-31 37.2781 11623796
2015-01-31 36.9748 7664340
2015-01-31 37.8848 9732554
2015-01-31 38.4961 13170548

For more info on time shifting visit http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.shift.html
Up next we'll look at rolling and expanding!

</html>