Consider the following data frame with a timestamp index which may have repeated (i.e. non-unique) index values, another column that indicates the asset, and another column with the value of that asset at that timestamp.
df
value asset
2021-03-18 11:00:00 4 A
2021-03-18 11:30:00 1 B
2021-03-18 12:00:00 3 A
2021-03-18 12:30:00 2 A
2021-03-18 13:00:00 3 A
2021-03-18 13:30:00 3 A
2021-03-18 14:00:00 1 A
2021-03-18 14:30:00 2 B
For each day, I would like to get the final value of each asset and subtract that from the value in each row, per asset. So in the above table, the last daily value for asset A is 1 (at time 2021-03-18 14:00:00), and for B is 2 (at time 2021-03-18 14:30:00). I would then like to deduct these values from the respective asset value in each row. So in the first row I want to calculate new_value
to equal 4-1 = 3, and for the second row to be 1-2 = -1.
How can I do that, taking into account that some index values may be repeated since they represent the time at which each asset was traded, and two assets may be traded at the same time.