Skip to content
Advertisement

Lag of values from 1 month ago

My initial dataset has only 2 columns, date and value.

What I’m trying to do is, for each date, get the value from the previous month (columns m-1 and m-12). The problems I’m having is when the day doesn’t exist in previous month, like 29 of February, that I want to leave it empty, and most methods tend to get the nearby dates.

So, the final table would be something like this:

date value m-1 m-12
2021-01-05 400 NaN NaN
2022-01-05 100 NaN 400
2022-01-28 300 NaN NaN
2022-02-05 300 100 NaN
2022-02-28 500 300 NaN
2022-03-29 300 NaN NaN

I was thinking I could use something like d.apply(lambda x: x['date'] - relativedelta(months = 1), axis=1) but with this, I only get the date, not the value. And it rounds the dates, for example for 2022-03-29 it returns 2022-02-28 which is not correct, it should be 02-29, and since it doesn’t exist it should be NaN.

Advertisement

Answer

Here is a possiblity :

JavaScript

Which outputs

date value m1 m12
0 2021-01-05 00:00:00 400 nan nan
1 2022-01-05 00:00:00 100 nan 400
2 2022-01-28 00:00:00 300 nan nan
3 2022-02-05 00:00:00 300 100 nan
4 2022-02-28 00:00:00 500 300 nan
5 2022-03-29 00:00:00 300 nan nan
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement