w3resource

Pandas Series: interpolate() function

Fill NA/missing values in a Pandas series

The interpolate() function is used to interpolate values according to different methods.

Syntax:

Series.interpolate(self, method='linear', axis=0, limit=None, inplace=False, limit_direction='forward', limit_area=None, downcast=None, **kwargs)
Pandas Series interpolate image

Parameters:

Name Description Type/Default Value Required / Optional
method Interpolation technique to use. One of:
  • ‘linear’: Ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes.
  • ‘time’: Works on daily and higher resolution data to interpolate given length of interval.
  • ‘index’, ‘values’: use the actual numerical values of the index.
  • ‘pad’: Fill in NaNs using existing values.
  • ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘spline’, ‘barycentric’, ‘polynomial’: Passed to scipy.interpolate.interp1d. These methods use the numerical values of the index. Both ‘polynomial’ and ‘spline’ require that you also specify an order (int), e.g. df.interpolate(method='polynomial', order=5).
  • ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’: Wrappers around the SciPy interpolation methods of similar names. See Notes.
  • ‘from_derivatives’: Refers to scipy.interpolate.BPoly.from_derivatives which replaces ‘piecewise_polynomial’ interpolation method in scipy 0.18.
New in version 0.18.1: Added support for the ‘akima’ method. Added interpolate method ‘from_derivatives’ which replaces ‘piecewise_polynomial’ in SciPy 0.18; backwards-compatible with SciPy < 0.18
str
Default Value: ‘linear’
Required
axis Axis to interpolate along. {0 or ‘index’, 1 or ‘columns’, None}
Default Value: None
Required
limit Maximum number of consecutive NaNs to fill. Must be greater than 0. int Optional
inplace Update the data in place if possible. bool
Default Value: False
Required
limit_direction If limit is specified, consecutive NaNs will be filled in this direction. {‘forward’, ‘backward’, ‘both’}
Default Value: ‘forward’
Required
limit_area

If limit is specified, consecutive NaNs will be filled with this restriction.

  • None: No fill restriction.
  • ‘inside’: Only fill NaNs surrounded by valid values (interpolate).
  • ‘outside’: Only fill NaNs outside valid values (extrapolate).
{None, ‘inside’, ‘outside’}
Default Value: None
Required
downcast Downcast dtypes if possible. infer’ or None
Default Value: None
Optional
**kwargs Keyword arguments to pass on to the interpolating function.   Required

Returns: Series or DataFrame- Returns the same object type as the caller, interpolated at some or all NaN values.

Notes

The ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’ methods are wrappers around the respective SciPy implementations of similar names. These use the actual numerical values of the index.

Example:


Download the above Notebook from here.



Inviting useful, relevant, well-written and unique guest posts