I have a dataframe with date as index and price in col 1. I want to add a second column that will take the 1 month difference in price. My problem is that some month have no value. If there's no value in the previous month I would like that it take the difference with the last price available.. But if at the month there's no price it keep no Value (nan). Is that possible ? Here's an exemple of my initial Dataframe
Date Price
2020-12-31 20
2021-01-31 25
2021-02-28 22
2021-03-31 nan
2021-04-30 32
2021-05-31 33
2021-06-30 nan
2021-07-31 40
Here's the result I would like. You can see that on march the price difference is nan because there's no price on that date, BUT next month on april the price difference is equal to april minus february because theres no price on march (32-22). Same exemple with Jully :
Date Price Price_difference
2020-12-31 20 nan
2021-01-31 25 5
2021-02-28 22 -3
2021-03-31 nan nan
2021-04-30 32 10
2021-05-31 34 2
2021-06-30 nan nan
2021-07-31 40 6
Here's my code now but it don't work... :
df_diff = df.diff(periods=1)