Sometimes, we may wish to shift the values in a time series backward or forward in time. One possible scenario is when a dataset contains the list of start dates of the new employees in a firm, and the company's HR program wishes to shift these dates forward by one year so that the employees' benefits can be activated. We can do this by using the shift() function as follows:
In [117]: ibmTS.shift(3) Out[117]: TradeDate 1959-06-29 NaN 1959-06-30 NaN 1959-07-01 NaN 1959-07-02 445 1959-07-06 448 1959-07-07 450 1959-07-08 447 ...
This shifts all the calendar days. However, if we wish to shift only business days, we must use the following command:
In [119]: ibmTS.shift(3, freq=pd.datetools.bday) Out[119]: TradeDate 1959-07-02 445 1959-07-03 448 1959-07-06 450 1959-07-07 447 1959-07-09 451
In the preceding snippet, we have specified the freq argument to shift; this tells the function to shift only the business days. The shift function has a freq argument whose value can be a DateOffset class, TimeDelta-like object, or an offset alias. Thus, using ibmTS.shift(3, freq='B') would also produce the same result.