Select Page

11.7 Shifting a Time Series


There are some situations in which an analyst may wish to shift a time series.  To shift a time series is to adjust its values in a consistent way, often to enable comparisons between the series’ original values and the ones that have been adjusted.  Someone analyzing a medical study might shift observed values in patient outcomes by 7 days, in order to see the effect of a new drug regimen.  Someone could shift a basketball team’s point totals one game forward, to see if particularly high- or low-scoring games are followed by similar results in the next outing.  

We have some share price data about Lobster Land’s competitor park, Turtle Town.  We will start by bringing it into our environment as a time series, as shown below, and checking out its first five rows:

Next, we shift the data forward by one period, and again examine its first five rows.  Note that the first value in the series has become NaN, since there was no previous value to move into its position.  For each of the other values here, we see the previous day’s data rather than the original data. 

We can change the number of days in the shift by simply adjusting the integer value passed to the shift() function.  Also, we can change the direction of the shift by passing a negative number, rather than a positive one.