October 2019
Beginner to intermediate
674 pages
16h 43m
English
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 ...
Read now
Unlock full access