# xarray.Dataset.interpolate_na

#### Dataset.interpolate_na(dim=None, method='linear', limit=None, use_coordinate=True, max_gap=None, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L6609-L6754)

Fill in NaNs by interpolating according to different methods.

* **Parameters:**
  * **dim** (`Hashable` or [`None`](https://docs.python.org/3/builtins/constants.html#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()`](https://numpy.org/doc/stable/reference/generated/numpy.interp.html#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`](https://docs.python.org/3/builtins/functions.html#bool) or `Hashable`, *default*: [`True`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#int), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#int), [`float`](https://docs.python.org/3/builtins/functions.html#float), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`pandas.Timedelta`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timedelta.html#pandas.Timedelta), [`numpy.timedelta64`](https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.timedelta64), datetime.timedelta             or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#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:
    - a string that is valid input for pandas.to_timedelta
    - a [`numpy.timedelta64`](https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.timedelta64) object
    - a [`pandas.Timedelta`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timedelta.html#pandas.Timedelta) object
    - a [`datetime.timedelta`](https://docs.python.org/3/library/datetime.html#datetime.timedelta) object

    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:
    ```default
    <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
  * **\*\*kwargs** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), *optional*) – parameters passed verbatim to the underlying interpolation function
* **Returns:**
  **interpolated** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Filled in Dataset.

#### WARNING
When passing fill_value as a keyword argument with method=”linear”, it does not use
`numpy.interp` but it uses `scipy.interpolate.interp1d`, which provides the fill_value parameter.

#### SEE ALSO
[`numpy.interp`](https://numpy.org/doc/stable/reference/generated/numpy.interp.html#numpy.interp), [`scipy.interpolate`](https://docs.scipy.org/doc/scipy/reference/interpolate.html#module-scipy.interpolate)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('012dbc13-4623-42b5-824d-0e544532a110','22799e17-ab58-49f7-9c74-cd1e394508b5','5c836cd0-8f4f-4265-9eb3-0f4028d7880c','../lite/tree/../notebooks/index.html?path=c57cbfea_0522_4cfa_ac22_9a90a619e5e6.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> ds = xr.Dataset(
...     {
...         "A": ("x", [np.nan, 2, 3, np.nan, 0]),
...         "B": ("x", [3, 4, np.nan, 1, 7]),
...         "C": ("x", [np.nan, np.nan, np.nan, 5, 0]),
...         "D": ("x", [np.nan, 3, np.nan, -1, 4]),
...     },
...     coords={"x": [0, 1, 2, 3, 4]},
... )
>>> ds
<xarray.Dataset> Size: 200B
Dimensions:  (x: 5)
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
Data variables:
    A        (x) float64 40B nan 2.0 3.0 nan 0.0
    B        (x) float64 40B 3.0 4.0 nan 1.0 7.0
    C        (x) float64 40B nan nan nan 5.0 0.0
    D        (x) float64 40B nan 3.0 nan -1.0 4.0
```

```pycon
>>> ds.interpolate_na(dim="x", method="linear")
<xarray.Dataset> Size: 200B
Dimensions:  (x: 5)
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
Data variables:
    A        (x) float64 40B nan 2.0 3.0 1.5 0.0
    B        (x) float64 40B 3.0 4.0 2.5 1.0 7.0
    C        (x) float64 40B nan nan nan 5.0 0.0
    D        (x) float64 40B nan 3.0 1.0 -1.0 4.0
```

```pycon
>>> ds.interpolate_na(dim="x", method="linear", fill_value="extrapolate")
<xarray.Dataset> Size: 200B
Dimensions:  (x: 5)
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
Data variables:
    A        (x) float64 40B 1.0 2.0 3.0 1.5 0.0
    B        (x) float64 40B 3.0 4.0 2.5 1.0 7.0
    C        (x) float64 40B 20.0 15.0 10.0 5.0 0.0
    D        (x) float64 40B 5.0 3.0 1.0 -1.0 4.0
```

<div id="5c836cd0-8f4f-4265-9eb3-0f4028d7880c" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('012dbc13-4623-42b5-824d-0e544532a110','5c836cd0-8f4f-4265-9eb3-0f4028d7880c')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('012dbc13-4623-42b5-824d-0e544532a110','5c836cd0-8f4f-4265-9eb3-0f4028d7880c')">Open In Tab</button></div><div id="22799e17-ab58-49f7-9c74-cd1e394508b5" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
