The interpolate() function

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:

Linear interpolation

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:

Backward interpolation with padding
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset