Understanding the interpolation technique
Interpolation is a technique used quite frequently in finance. In the following example, we have to replace two missing values, NaN
, between 2 and 6. The pandas.interpolate()
function, for a linear interpolation, is used to fill in the two missing values:
import pandas as pd import numpy as np nn=np.nan x=pd.Series([1,2,nn,nn,6]) print(x.interpolate())
The output is shown here:
0 1.000000 1 2.000000 2 3.333333 3 4.666667 4 6.000000 dtype: float64
The preceding method is a linear interpolation. Actually, we could estimate a Δ and calculate those missing values manually:
Here, v2(v1) is the second (first) value ...
Get Python for Finance - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.