Interpolate a date between two other dates to get a value

Viewed 5193

I have this pandas dataframe:

     ISIN             MATURITY       PRICE   
 0  AR121489 Corp     29/09/2019    5.300
 1  AR714081 Corp     29/12/2019    7.500
 2  AT452141 Corp     29/06/2020    2.950
 3  QJ100923 Corp     29/09/2020    6.662

My question is if there exists a way to interpolate a date in the column "MATURITY" and get the price value of that date. For example, If I select the date 18/11/2019, the value of the price on that date should be between 5.300 and 7.500. I don't know if what I am asking is possible but thank you so much for taking your time to read it and trying to help me.

2 Answers

What you can do if you wanted a daily frequency interpolated is first create a daily frequency range with your start and end-dates.

new_df = pd.DataFrame()
new_df["MATURITY"] = pd.date_range(start='29/09/2019', end='29/09/2020')
new_df = pd.concat([new_df,old_df], join="outer", axis=1)
new_df["PRICE"] = new_df["PRICE"].interpolate(method = "linear")

I would treat the dates as datetime objects and for interpolation convert the date from datetime object to some time-interval value i.e. either seconds since 20XX-XX-XX 00:00:00 or days and the same I would do for the output timemoments. After that the interpolation works also with NumPy interpolate method.

In matplotlib.dates there is a method date2num and also num2date worth to try.

Related