简体   繁体   English

根据现有时间序列数据进行插值-Python

[英]Interpolate from Existing Time-series Data - Python

I have a time-series of dates and interest rates on a pandas dataframe. 我在熊猫数据框上有一个日期和利率的时间序列。 Below is the list: 以下是列表:

        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

I'd like to interpolate a rate for any date (for example - 03/21/2021) that falls b/n the min and max dates. 我想为任何日期(例如,最小日期和最大日期)下降的比率(例如03/21/2021)进行插值。

And I'd like to achieve this using the interpolate method of pandas. 我想使用熊猫的插值方法来实现这一目标。 How can I do it? 我该怎么做?

I will recommend numpy.interp , here I am convert date type to numeric 我将推荐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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM