简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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