How to interpolate for x date using Python

Viewed 161

I have df with the endDate column, which I want to use as my x array and discountFactor column as y array. The goal is to interpolate discountFactor at some date within the range of x array, let's say 2020-12-31 using cubic spline method.

The source df below:

lst = [['6M', '2020-10-26', '2021-04-26', '-0.521684','1.002611'], ['1Y', '2020-10-26', '2021-10-26', '-0.534855','1.005377'], 
       ['5Y', '2020-10-26', '2025-10-27','-0.495927','1.025184']] 
    
df = pd.DataFrame(lst, columns =['tenor', 'startDate', 'endDate','ratePercent','discountFactor'], dtype = float)

Please see the following steps, which unfortunately lead to df with all NaN columns, even there where I had known discountFacor values

# Converting date column strings to date objects
df['endDate'] = pd.to_datetime(df['endDate'], format='%Y-%m-%d')
df['startDate'] = pd.to_datetime(df['startDate'], format='%Y-%m-%d')
# Setting endDate column as my index
df.set_index("endDate", inplace= True)
# Creating daily dates range between start and end dates from the df
dates_range = pd.date_range(start='2020-10-26', end='2025-10-27')
idx = pd.DatetimeIndex(dates_range)
df = df.reindex(idx)
0 Answers
Related