I need to calculate XNPV of cash flows at different future dates. Is there any function to do this in numpy, pandas or plain python?
Consider a dataframe like so:
import pandas as pd
df = pd.DataFrame({'ptf_id': [1,1,1,1,1],
'date': pd.date_range("2022-06-05", periods=5, freq="M"),
'cf': [10000,12000, 8000,7000,11000],
'rate': [0.12,0.12,0.12,0.12,0.12]})
df = df.append(pd.DataFrame(({'ptf_id': [2,2,2,2,2],
'date': pd.date_range("2022-07-11", periods=5, freq="M"),
'cf': [15000,12000, 10000,8000,7000],
'rate': [0.15,0.15,0.15,0.15,0.15]})))
ptf_id date cf rate
1 2022-06-30 10000 0.12
1 2022-07-31 12000 0.12
1 2022-08-31 8000 0.12
1 2022-09-30 7000 0.12
1 2022-10-31 11000 0.12
2 2022-07-31 15000 0.15
2 2022-08-31 12000 0.15
2 2022-09-30 10000 0.15
2 2022-10-31 8000 0.15
2 2022-11-30 7000 0.15
Is it possible to calculate the XNPV by ptf_id at different future dates, such that the result looks like this:
ptf_id date cf rate xnpv
1 2022-06-30 10000 0.12 37123
1 2022-07-31 12000 0.12 25482
1 2022-08-31 8000 0.12 17729
1 2022-09-30 7000 0.12 10895
1 2022-10-31 11000 0.12 0
2 2022-07-31 15000 0.15 36031
2 2022-08-31 12000 0.15 24461
2 2022-09-30 10000 0.15 14744
2 2022-10-31 8000 0.15 6920
2 2022-11-30 7000 0.15 0