Home > Back-end >  Create lagged column in pandas based on a date column
Create lagged column in pandas based on a date column

Time:08-13

I have this df:

  PredictionDate    U.S. 30 yr FRM  U.S. 15 yr FRM
0      2014-12-31            3.87          3.15
1      2015-01-01            3.87          3.15
2      2015-01-02            3.87          3.15
3      2015-01-03            3.87          3.15
4      2015-01-04            3.87          3.15
               ...  ... ... ...
2769    2022-07-31           5.30          4.58
2770    2022-08-01           4.99          4.26
2771    2022-08-02           4.99          4.26
2772    2022-08-03           4.99          4.26
2773    2022-08-04           4.99          4.26

And would like to create two new columns that contain the data for 30yr and 15yr from 30 days prior to the date for that row in the PredictedDate column. The output should look like this:

   PredictionDate   U.S. 30 yr FRM  U.S. 15 yr FRM      30yrLag 15yrLag
0      2014-12-31            3.87           3.15        3.72  3.12
1      2015-01-01            3.87           3.15        3.72  3.12
2      2015-01-02            3.87           3.15        4.72  3.12
3      2015-01-03            3.87           3.15         .     .
4      2015-01-04            3.87           3.15         .     .
                  ... ... ... ...
20528   2022-07-31           5.30           4.58         .     .
20529   2022-08-01           4.99           4.26         .     .
20530   2022-08-02           4.99           4.26         .     .
20531   2022-08-03           4.99           4.26         .     .
20532   2022-08-04           4.99           4.26         .     .

Any ideas?

CodePudding user response:

Solution:

pmms_df.set_index('PredictionDate', inplace=True)
pmms_df['30yrLag'] = pmms_df.shift(30)['U.S. 30 yr FRM'].values
pmms_df['15yrLag'] = pmms_df.shift(30)['U.S. 15 yr FRM'].values
pmms_df.reset_index(inplace = True)
  • Related