🍾 Xarray is now 10 years old! 🎉

xarray.DataArray.interpolate_na

xarray.DataArray.interpolate_na#

DataArray.interpolate_na(dim=None, method='linear', limit=None, use_coordinate=True, max_gap=None, keep_attrs=None, **kwargs)[source]#

Fill in NaNs by interpolating according to different methods.

Parameters:
  • dim (Hashable or None, optional) – Specifies the dimension along which to interpolate.

  • method ({"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial", "barycentric", "krogh", "pchip", "spline", "akima"}, default: "linear") – String indicating which method to use for interpolation:

    • ‘linear’: linear interpolation. Additional keyword arguments are passed to numpy.interp()

    • ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘polynomial’: are passed to scipy.interpolate.interp1d(). If method='polynomial', the order keyword argument must also be provided.

    • ‘barycentric’, ‘krogh’, ‘pchip’, ‘spline’, ‘akima’: use their respective scipy.interpolate classes.

  • use_coordinate (bool or str, default: True) – Specifies which index to use as the x values in the interpolation formulated as y = f(x). If False, values are treated as if equally-spaced along dim. If True, the IndexVariable dim is used. If use_coordinate is a string, it specifies the name of a coordinate variable to use as the index.

  • limit (int or None, default: None) – Maximum number of consecutive NaNs to fill. Must be greater than 0 or None for no limit. This filling is done regardless of the size of the gap in the data. To only interpolate over gaps less than a given length, see max_gap.

  • max_gap (int, float, str, pandas.Timedelta, numpy.timedelta64, datetime.timedelta, default: None) – Maximum size of gap, a continuous sequence of NaNs, that will be filled. Use None for no limit. When interpolating along a datetime64 dimension and use_coordinate=True, max_gap can be one of the following:

    Otherwise, max_gap must be an int or a float. Use of max_gap with unlabeled dimensions has not been implemented yet. Gap length is defined as the difference between coordinate values at the first data point after a gap and the last value before a gap. For gaps at the beginning (end), gap length is defined as the difference between coordinate values at the first (last) valid data point and the first (last) NaN. For example, consider:

    <xarray.DataArray (x: 9)>
    array([nan, nan, nan,  1., nan, nan,  4., nan, nan])
    Coordinates:
      * x        (x) int64 0 1 2 3 4 5 6 7 8
    

    The gap lengths are 3-0 = 3; 6-3 = 3; and 8-6 = 2 respectively

  • keep_attrs (bool or None, default: None) – If True, the dataarray’s attributes (attrs) will be copied from the original object to the new one. If False, the new object will be returned without attributes.

  • **kwargs (dict, optional) – parameters passed verbatim to the underlying interpolation function

Returns:

interpolated (DataArray) – Filled in DataArray.

Examples

>>> da = xr.DataArray(
...     [np.nan, 2, 3, np.nan, 0], dims="x", coords={"x": [0, 1, 2, 3, 4]}
... )
>>> da
<xarray.DataArray (x: 5)> Size: 40B
array([nan,  2.,  3., nan,  0.])
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
>>> da.interpolate_na(dim="x", method="linear")
<xarray.DataArray (x: 5)> Size: 40B
array([nan, 2. , 3. , 1.5, 0. ])
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
>>> da.interpolate_na(dim="x", method="linear", fill_value="extrapolate")
<xarray.DataArray (x: 5)> Size: 40B
array([1. , 2. , 3. , 1.5, 0. ])
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4