从现有的时间序列数据插值 - Python

时间:2018-02-28 18:58:33

标签: python pandas interpolation finance quantitative-finance

我在熊猫数据框中有一系列日期和利率。以下是清单:

        dates     rates
0    3/1/2018  0.014553
1    3/8/2018  0.014951
2    4/2/2018  0.016987
3    5/1/2018  0.018719
4    6/1/2018  0.020044
5    9/4/2018  0.021602
6   12/3/2018  0.022361
7    3/1/2019  0.023080
8    6/3/2019  0.023726
9    9/3/2019  0.024333
10  12/2/2019  0.024811
11   3/2/2020  0.025234
12   3/1/2021  0.026456
13   3/1/2022  0.027126
14   3/1/2023  0.027541
15   3/1/2024  0.027898
16   3/3/2025  0.028206
17   3/2/2026  0.028486
18   3/1/2027  0.028748
19   3/1/2028  0.028998
20   3/1/2030  0.029444
21   3/1/2033  0.029850
22   3/1/2038  0.030126
23   3/2/2043  0.030019
24   3/2/2048  0.029778

我想插入任何日期(例如 - 03/21/2021)的费率,该费率是最小和最大日期的b / n。

我想使用pandas的interpolate方法实现这一目标。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我会推荐numpy.interp,这里我将日期类型转换为数字

np.interp(pd.to_numeric(pd.Series(pd.to_datetime('03/21/2021'))).values,pd.to_numeric(df['dates']).values,df['rates'].values)
Out[425]: array([0.02649271])