# xarray.Dataset.reindex

#### Dataset.reindex(indexers=None, method=None, tolerance=None, copy=True, fill_value=<NA>, \*\*indexers_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L3554-L3768)

Conform this object onto a new set of indexes, filling in
missing values with `fill_value`. The default fill value is NaN.

* **Parameters:**
  * **indexers** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), *optional*) – Dictionary with keys given by dimension names and values given by
    arrays of coordinates tick labels. Any mismatched coordinate
    values will be filled in with NaN, and any mismatched dimension
    names will simply be ignored.
    One of indexers or indexers_kwargs must be provided.
  * **method** (`{None, "nearest", "pad", "ffill", "backfill", "bfill", None}`, *optional*) – Method to use for filling index values in `indexers` not found in
    this dataset:
    - None (default): don’t fill gaps
    - “pad” / “ffill”: propagate last valid index value forward
    - “backfill” / “bfill”: propagate next valid index value backward
    - “nearest”: use nearest valid index value
  * **tolerance** (`float | Iterable[float] | str | None`, *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – Maximum distance between original and new labels for inexact
    matches. The values of the index at the matching locations must
    satisfy the equation `abs(index[indexer] - target) <= tolerance`.
    Tolerance may be a scalar value, which applies the same tolerance
    to all values, or list-like, which applies variable tolerance per
    element. List-like must be the same size as the index and its dtype
    must exactly match the index’s type.
  * **copy** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If `copy=True`, data in the return value is always copied. If
    `copy=False` and reindexing is unnecessary, or can be performed
    with only slice operations, then the output may share memory with
    the input. In either case, a new xarray object is always returned.
  * **fill_value** ([scalar](../user-guide/terminology.html.md#term-scalar) or [dict-like](https://docs.python.org/3/glossary.html#term-mapping), *optional*) – Value to use for newly missing values. If a dict-like,
    maps variable names (including coordinates) to fill values.
  * **sparse** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – use sparse-array.
  * **\*\*indexers_kwargs** (`{dim: indexer, ...}`, *optional*) – Keyword arguments in the same form as `indexers`.
    One of indexers or indexers_kwargs must be provided.
* **Returns:**
  **reindexed** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Another dataset, with this dataset’s data but replaced coordinates.

#### SEE ALSO
[`Dataset.reindex_like`](xarray.Dataset.reindex_like.html.md#xarray.Dataset.reindex_like), [`align`](xarray.align.html.md#xarray.align), [`pandas.Index.get_indexer`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.get_indexer.html#pandas.Index.get_indexer)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('3ccc7a29-fcf7-4eba-983c-da76744e7e82','a2503713-5ada-425b-a2dd-ce84c078b902','a2000070-2ef8-48ee-b517-5d99407ce634','../lite/tree/../notebooks/index.html?path=fd8fc21a_8d2e_48ba_9c4d_c22d6c2477fe.ipynb','None')">Try it in your browser!</button></div>

Create a dataset with some fictional data.

```pycon
>>> x = xr.Dataset(
...     {
...         "temperature": ("station", 20 * np.random.rand(4)),
...         "pressure": ("station", 500 * np.random.rand(4)),
...     },
...     coords={"station": ["boston", "nyc", "seattle", "denver"]},
... )
>>> x
<xarray.Dataset> Size: 176B
Dimensions:      (station: 4)
Coordinates:
  * station      (station) <U7 112B 'boston' 'nyc' 'seattle' 'denver'
Data variables:
    temperature  (station) float64 32B 10.98 14.3 12.06 10.9
    pressure     (station) float64 32B 211.8 322.9 218.8 445.9
>>> x.indexes
Indexes:
    station  Index(['boston', 'nyc', 'seattle', 'denver'], dtype='str', name='station')
```

Create a new index and reindex the dataset. By default values in the new index that
do not have corresponding records in the dataset are assigned NaN.

```pycon
>>> new_index = ["boston", "austin", "seattle", "lincoln"]
>>> x.reindex({"station": new_index})
<xarray.Dataset> Size: 176B
Dimensions:      (station: 4)
Coordinates:
  * station      (station) <U7 112B 'boston' 'austin' 'seattle' 'lincoln'
Data variables:
    temperature  (station) float64 32B 10.98 nan 12.06 nan
    pressure     (station) float64 32B 211.8 nan 218.8 nan
```

We can fill in the missing values by passing a value to the keyword fill_value.

```pycon
>>> x.reindex({"station": new_index}, fill_value=0)
<xarray.Dataset> Size: 176B
Dimensions:      (station: 4)
Coordinates:
  * station      (station) <U7 112B 'boston' 'austin' 'seattle' 'lincoln'
Data variables:
    temperature  (station) float64 32B 10.98 0.0 12.06 0.0
    pressure     (station) float64 32B 211.8 0.0 218.8 0.0
```

We can also use different fill values for each variable.

```pycon
>>> x.reindex(
...     {"station": new_index}, fill_value={"temperature": 0, "pressure": 100}
... )
<xarray.Dataset> Size: 176B
Dimensions:      (station: 4)
Coordinates:
  * station      (station) <U7 112B 'boston' 'austin' 'seattle' 'lincoln'
Data variables:
    temperature  (station) float64 32B 10.98 0.0 12.06 0.0
    pressure     (station) float64 32B 211.8 100.0 218.8 100.0
```

Because the index is not monotonically increasing or decreasing, we cannot use arguments
to the keyword method to fill the NaN values.

```pycon
>>> x.reindex({"station": new_index}, method="nearest")
Traceback (most recent call last):
...
    raise ValueError('index must be monotonic increasing or decreasing')
ValueError: index must be monotonic increasing or decreasing
```

To further illustrate the filling functionality in reindex, we will create a
dataset with a monotonically increasing index (for example, a sequence of dates).

```pycon
>>> x2 = xr.Dataset(
...     {
...         "temperature": (
...             "time",
...             [15.57, 12.77, np.nan, 0.3081, 16.59, 15.12],
...         ),
...         "pressure": ("time", 500 * np.random.rand(6)),
...     },
...     coords={"time": pd.date_range("01/01/2019", periods=6, freq="D")},
... )
>>> x2
<xarray.Dataset> Size: 144B
Dimensions:      (time: 6)
Coordinates:
  * time         (time) datetime64[us] 48B 2019-01-01 2019-01-02 ... 2019-01-06
Data variables:
    temperature  (time) float64 48B 15.57 12.77 nan 0.3081 16.59 15.12
    pressure     (time) float64 48B 481.8 191.7 395.9 264.4 284.0 462.8
```

Suppose we decide to expand the dataset to cover a wider date range.

```pycon
>>> time_index2 = pd.date_range("12/29/2018", periods=10, freq="D")
>>> x2.reindex({"time": time_index2})
<xarray.Dataset> Size: 240B
Dimensions:      (time: 10)
Coordinates:
  * time         (time) datetime64[us] 80B 2018-12-29 2018-12-30 ... 2019-01-07
Data variables:
    temperature  (time) float64 80B nan nan nan 15.57 ... 0.3081 16.59 15.12 nan
    pressure     (time) float64 80B nan nan nan 481.8 ... 264.4 284.0 462.8 nan
```

The index entries that did not have a value in the original data frame (for example, 2018-12-29)
are by default filled with NaN. If desired, we can fill in the missing values using one of several options.

For example, to back-propagate the last valid value to fill the NaN values,
pass bfill as an argument to the method keyword.

```pycon
>>> x3 = x2.reindex({"time": time_index2}, method="bfill")
>>> x3
<xarray.Dataset> Size: 240B
Dimensions:      (time: 10)
Coordinates:
  * time         (time) datetime64[us] 80B 2018-12-29 2018-12-30 ... 2019-01-07
Data variables:
    temperature  (time) float64 80B 15.57 15.57 15.57 15.57 ... 16.59 15.12 nan
    pressure     (time) float64 80B 481.8 481.8 481.8 481.8 ... 284.0 462.8 nan
```

Please note that the NaN value present in the original dataset (at index value 2019-01-03)
will not be filled by any of the value propagation schemes.

```pycon
>>> x2.where(x2.temperature.isnull(), drop=True)
<xarray.Dataset> Size: 24B
Dimensions:      (time: 1)
Coordinates:
  * time         (time) datetime64[us] 8B 2019-01-03
Data variables:
    temperature  (time) float64 8B nan
    pressure     (time) float64 8B 395.9
>>> x3.where(x3.temperature.isnull(), drop=True)
<xarray.Dataset> Size: 48B
Dimensions:      (time: 2)
Coordinates:
  * time         (time) datetime64[us] 16B 2019-01-03 2019-01-07
Data variables:
    temperature  (time) float64 16B nan nan
    pressure     (time) float64 16B 395.9 nan
```

This is because filling while reindexing does not look at dataset values, but only compares
the original and desired indexes. If you do want to fill in the NaN values present in the
original dataset, use the [`fillna()`](xarray.Dataset.fillna.html.md#xarray.Dataset.fillna) method.

<div id="a2000070-2ef8-48ee-b517-5d99407ce634" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('3ccc7a29-fcf7-4eba-983c-da76744e7e82','a2000070-2ef8-48ee-b517-5d99407ce634')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('3ccc7a29-fcf7-4eba-983c-da76744e7e82','a2000070-2ef8-48ee-b517-5d99407ce634')">Open In Tab</button></div><div id="a2503713-5ada-425b-a2dd-ce84c078b902" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
