Super Simple Primer on Interpolation in Python
This is a super simple primer on interpolation. Less a tutorial, more a quick reminder when you need to do some interpolation quickly.
Super Simple Primer on Interpolation in Python
This is a super simple primer on interpolation. Less a tutorial, more a quick reminder when you need to do some interpolation quickly.
First, why interpolate?
That’s because there are always gaps in data, so interpolation is essential for data analysis. In finance, interpolation is especially useful when we are dealing with time series data.
Let’s first generate a series of data with gaps.
timerange = pd.date_range('1/1/2016', periods=252)
mean = 1.2
std = 0.3
t_series = pd.DataFrame(np.random.normal(mean, std ,len(timerange)),
index=timerange, columns=['Prices'])
t_series['Prices_Gapped'] = t_series['Prices']
t_series.loc[np.random.choice(t_series.index, 80), 'Prices_Gapped'] = np.nanThe last line above may be a little new. But all it does it to help us choose random spots to insert 80 gaps.
Now, to fill in these gaps by interpolation, one line will do it.
t_series['Prices_Filled']=t_series['Prices_Gapped'].interpolate(method='linear')
There are other options. We can also use a polynomial of order 2 for the interpolation.
t_series['Prices_Filled_Poly']=t_series['Prices_Gapped'].interpolate(method='polynomial', order=2)
Many more options are available. Just check out the reference here.
The notebook on this can be found here.

