# xarray.align

### xarray.align(\*objects, join='inner', copy=True, indexes=None, exclude=frozenset({}), fill_value=<NA>)[[source]](https://github.com/pydata/xarray/blob/main/xarray/structure/alignment.py#L778-L983)

Given any number of Dataset and/or DataArray objects, returns new
objects with aligned indexes and dimension sizes.

Array from the aligned objects are suitable as input to mathematical
operators, because along each dimension they have the same index and size.

Missing values (if `join != 'inner'`) are filled with `fill_value`.
The default fill value is NaN.

* **Parameters:**
  * **\*objects** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset) or [`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Objects to align.
  * **join** (`{"outer", "inner", "left", "right", "exact", "override"}`, *optional*) – Method for joining the indexes of the passed objects along each
    dimension:
    - “outer”: use the union of object indexes
    - “inner”: use the intersection of object indexes
    - “left”: use indexes from the first object with each dimension
    - “right”: use indexes from the last object with each dimension
    - “exact”: instead of aligning, raise ValueError when indexes to be
      aligned are not equal
    - “override”: if indexes are of same size, rewrite indexes to be
      those of the first object with that dimension. Indexes for the same
      dimension must have the same size in all objects.
  * **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 values 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, new xarray objects are always returned.
  * **indexes** ([dict-like](https://docs.python.org/3/glossary.html#term-mapping), *optional*) – Any indexes explicitly provided with the indexes argument should be
    used in preference to the aligned indexes.
  * **exclude** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [iterable](https://docs.python.org/3/glossary.html#term-iterable) of [hashable](../user-guide/terminology.html.md#term-name) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dimensions that must be excluded from alignment
  * **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 to fill values. Use a data array’s name to
    refer to its values.
* **Returns:**
  **aligned** ([`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Tuple of objects with the same type as \*objects with aligned
  coordinates.
* **Raises:**
  [**AlignmentError**](xarray.AlignmentError.html.md#xarray.AlignmentError) – If any dimensions without labels on the arguments have different sizes,
      or a different size than the size of the aligned dimension labels.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('3b15fd62-bac7-49ee-8ab7-7fdfeaebcbe4','f45bb0f3-2aca-4fa4-b222-4f512d333a93','dd5add19-1495-46ab-adac-c989c4609232','../lite/tree/../notebooks/index.html?path=13045209_8b54_491f_b05a_d9365cff89a0.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> x = xr.DataArray(
...     [[25, 35], [10, 24]],
...     dims=("lat", "lon"),
...     coords={"lat": [35.0, 40.0], "lon": [100.0, 120.0]},
... )
>>> y = xr.DataArray(
...     [[20, 5], [7, 13]],
...     dims=("lat", "lon"),
...     coords={"lat": [35.0, 42.0], "lon": [100.0, 120.0]},
... )
```

```pycon
>>> x
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[25, 35],
       [10, 24]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> y
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[20,  5],
       [ 7, 13]])
Coordinates:
  * lat      (lat) float64 16B 35.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y)
>>> a
<xarray.DataArray (lat: 1, lon: 2)> Size: 16B
array([[25, 35]])
Coordinates:
  * lat      (lat) float64 8B 35.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 1, lon: 2)> Size: 16B
array([[20,  5]])
Coordinates:
  * lat      (lat) float64 8B 35.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y, join="outer")
>>> a
<xarray.DataArray (lat: 3, lon: 2)> Size: 48B
array([[25., 35.],
       [10., 24.],
       [nan, nan]])
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 3, lon: 2)> Size: 48B
array([[20.,  5.],
       [nan, nan],
       [ 7., 13.]])
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y, join="outer", fill_value=-999)
>>> a
<xarray.DataArray (lat: 3, lon: 2)> Size: 48B
array([[  25,   35],
       [  10,   24],
       [-999, -999]])
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 3, lon: 2)> Size: 48B
array([[  20,    5],
       [-999, -999],
       [   7,   13]])
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y, join="left")
>>> a
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[25, 35],
       [10, 24]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[20.,  5.],
       [nan, nan]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y, join="right")
>>> a
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[25., 35.],
       [nan, nan]])
Coordinates:
  * lat      (lat) float64 16B 35.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[20,  5],
       [ 7, 13]])
Coordinates:
  * lat      (lat) float64 16B 35.0 42.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> a, b = xr.align(x, y, join="exact")
Traceback (most recent call last):
...
xarray.structure.alignment.AlignmentError: cannot align objects with join='exact' ...
```

```pycon
>>> a, b = xr.align(x, y, join="override")
>>> a
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[25, 35],
       [10, 24]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
>>> b
<xarray.DataArray (lat: 2, lon: 2)> Size: 32B
array([[20,  5],
       [ 7, 13]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
```

<div id="dd5add19-1495-46ab-adac-c989c4609232" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('3b15fd62-bac7-49ee-8ab7-7fdfeaebcbe4','dd5add19-1495-46ab-adac-c989c4609232')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('3b15fd62-bac7-49ee-8ab7-7fdfeaebcbe4','dd5add19-1495-46ab-adac-c989c4609232')">Open In Tab</button></div><div id="f45bb0f3-2aca-4fa4-b222-4f512d333a93" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
