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 and n is the number of intervals between those two values. For the preceding case, Δ is (6-2)/3=1.33333. Thus, the next value will be v1+Δ=2+1.33333=3.33333. This way, we could continually estimate all missing values. Note that if we have several periods with missing values, then the delta for each period has to be calculated manually...