pandas.api.extensions.ExtensionArray.interpolate#

ExtensionArray.interpolate(*, method, axis, index, limit, limit_direction, limit_area, copy, **kwargs)[source]#

Fill NaN values using an interpolation method.

Parameters:
methodstr, default ‘linear’

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. * ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘polynomial’: Passed to scipy.interpolate.interp1d, whereas ‘spline’ is passed to scipy.interpolate.UnivariateSpline. These methods use the numerical values of the index. Both ‘polynomial’ and ‘spline’ require that you also specify an order (int), e.g. arr.interpolate(method=’polynomial’, order=5). * ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’, ‘cubicspline’: Wrappers around the SciPy interpolation methods of similar names. See Notes. * ‘from_derivatives’: Refers to scipy.interpolate.BPoly.from_derivatives.

axisint

Axis to interpolate along. For 1-dimensional data, use 0.

indexIndex

Index to use for interpolation.

limitint or None

Maximum number of consecutive NaNs to fill. Must be greater than 0.

limit_direction{‘forward’, ‘backward’, ‘both’}

Consecutive NaNs will be filled in this direction.

limit_area{‘inside’, ‘outside’} or None

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).

copybool

If True, a copy of the object is returned with interpolated values.

**kwargsoptional

Keyword arguments to pass on to the interpolating function.

Returns:
ExtensionArray

An ExtensionArray with interpolated values.

See also

Series.interpolate

Interpolate values in a Series.

DataFrame.interpolate

Interpolate values in a DataFrame.

Notes

  • All parameters must be specified as keyword arguments.

  • 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.

Examples

Interpolating values in a NumPy array:

>>> arr = pd.arrays.NumpyExtensionArray(np.array([0, 1, np.nan, 3]))
>>> arr.interpolate(
...     method="linear",
...     limit=3,
...     limit_direction="forward",
...     index=pd.Index(range(len(arr))),
...     fill_value=1,
...     copy=False,
...     axis=0,
...     limit_area="inside",
... )
<NumpyExtensionArray>
[0.0, 1.0, 2.0, 3.0]
Length: 4, dtype: float64

Interpolating values in a FloatingArray:

>>> arr = pd.array([1.0, pd.NA, 3.0, 4.0, pd.NA, 6.0], dtype="Float64")
>>> arr.interpolate(
...     method="linear",
...     axis=0,
...     index=pd.Index(range(len(arr))),
...     limit=None,
...     limit_direction="both",
...     limit_area=None,
...     copy=True,
... )
<FloatingArray>
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Length: 6, dtype: Float64