如何使用Python为x日期插值

2024-04-26 18:51:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有dfendDate列,我想用它作为我的x数组和discountFactor列作为y数组。目标是使用三次样条方法在x数组范围内的某个日期内插值discountFactor,比如2020-12-31

源代码df如下:

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)

请参阅以下步骤,不幸的是,这些步骤导致了所有df列的NaN,即使在我知道discountFacor值的地方也是如此

# 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)