The interpolate() function provides an efficient way to handle missing data. Through this method, the NaNs can be replaced with a value through linear interpolation or polynomial interpolation, or even simple padding. This function fits the series to a function such as a spline or quadratic and then computes the possible missing data.
Consider the following series:
lin_series = pd.Series([17,19,np.NaN,23,25,np.NaN,29])
Since the values are all equally spaced apart, linear interpolation is the most suitable method here. Linear interpolation is the default value of the method parameter of the interpolate function:
lin_series.interpolate()
The following is the output:
The direction in which interpolation should take place can be specified. Let's consider the preceding example and fill in the NaNs through backward padding, as shown here:
lin_series.interpolate(method = "pad", limit_direction = "backward")
The following is the output: