# xarray.merge

### xarray.merge(objects, compat=no_conflicts, join=outer, fill_value=<NA>, combine_attrs='override')[[source]](https://github.com/pydata/xarray/blob/main/xarray/structure/merge.py#L874-L1145)

Merge any number of xarray objects into a single Dataset as variables.

* **Parameters:**
  * **objects** ([iterable](https://docs.python.org/3/glossary.html#term-iterable) of [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [`Dataset`](xarray.Dataset.html.md#xarray.Dataset), [`DataTree`](xarray.DataTree.html.md#xarray.DataTree) or [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Merge together all variables from these objects. If any of them are
    DataArray objects, they must have a name.
  * **compat** (`{"identical", "equals", "broadcast_equals", "no_conflicts", "override", "minimal"}`, *default*: `"no_conflicts"`) – String indicating how to compare variables of the same name for
    potential conflicts:
    - “identical”: all values, dimensions and attributes must be the
      same.
    - “equals”: all values and dimensions must be the same.
    - “broadcast_equals”: all values must be equal when variables are
      broadcast against each other to ensure common dimensions.
    - “no_conflicts”: only values which are not null in both datasets
      must be equal. The returned dataset then contains the combination
      of all non-null values.
    - “override”: skip comparing and pick variable from first dataset
    - “minimal”: drop conflicting coordinates
  * **join** (`{"outer", "inner", "left", "right", "exact", "override"}`, *default*: `"outer"`) – String indicating how to combine differing indexes in objects.
    - “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.
  * **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.
  * **combine_attrs** (`{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}` or [`callable()`](https://docs.python.org/3/builtins/functions.html#callable), *default*: `"override"`) – A callable or a string indicating how to combine attrs of the objects being
    merged:
    - “drop”: empty attrs on returned Dataset.
    - “identical”: all attrs must be the same on every object.
    - “no_conflicts”: attrs from all objects are combined, any that have
      the same name must also have the same value.
    - “drop_conflicts”: attrs from all objects are combined, any that have
      the same name but different values are dropped.
    - “override”: skip comparing and copy attrs from the first dataset to
      the result.

    If a callable, it must expect a sequence of `attrs` dicts and a context object
    as its only parameters.
* **Returns:**
  [`Dataset`](xarray.Dataset.html.md#xarray.Dataset) or [`DataTree`](xarray.DataTree.html.md#xarray.DataTree) – Objects with combined variables from the inputs. If any inputs are a
  DataTree, this will also be a DataTree. Otherwise it will be a Dataset.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('22210e06-b656-4626-a896-16ce389049c6','ad1ad89d-f43a-4b79-8171-0adaf017aae9','2d951b56-ff3e-4fa5-b40a-2c8084a2650e','../lite/tree/../notebooks/index.html?path=d13b2224_3de0_495a_b49c_07b7f9bfa962.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> x = xr.DataArray(
...     [[1.0, 2.0], [3.0, 5.0]],
...     dims=("lat", "lon"),
...     coords={"lat": [35.0, 40.0], "lon": [100.0, 120.0]},
...     name="var1",
... )
>>> y = xr.DataArray(
...     [[5.0, 6.0], [7.0, 8.0]],
...     dims=("lat", "lon"),
...     coords={"lat": [35.0, 42.0], "lon": [100.0, 150.0]},
...     name="var2",
... )
>>> z = xr.DataArray(
...     [[0.0, 3.0], [4.0, 9.0]],
...     dims=("time", "lon"),
...     coords={"time": [30.0, 60.0], "lon": [100.0, 150.0]},
...     name="var3",
... )
```

```pycon
>>> x
<xarray.DataArray 'var1' (lat: 2, lon: 2)> Size: 32B
array([[1., 2.],
       [3., 5.]])
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
```

```pycon
>>> y
<xarray.DataArray 'var2' (lat: 2, lon: 2)> Size: 32B
array([[5., 6.],
       [7., 8.]])
Coordinates:
  * lat      (lat) float64 16B 35.0 42.0
  * lon      (lon) float64 16B 100.0 150.0
```

```pycon
>>> z
<xarray.DataArray 'var3' (time: 2, lon: 2)> Size: 32B
array([[0., 3.],
       [4., 9.]])
Coordinates:
  * time     (time) float64 16B 30.0 60.0
  * lon      (lon) float64 16B 100.0 150.0
```

```pycon
>>> xr.merge([x, y, z], join="outer")
<xarray.Dataset> Size: 256B
Dimensions:  (lat: 3, lon: 3, time: 2)
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 24B 100.0 120.0 150.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 72B 1.0 2.0 nan 3.0 5.0 nan nan nan nan
    var2     (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0
    var3     (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0
```

```pycon
>>> xr.merge([x, y, z], compat="identical", join="outer")
<xarray.Dataset> Size: 256B
Dimensions:  (lat: 3, lon: 3, time: 2)
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 24B 100.0 120.0 150.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 72B 1.0 2.0 nan 3.0 5.0 nan nan nan nan
    var2     (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0
    var3     (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0
```

```pycon
>>> xr.merge([x, y, z], compat="equals", join="outer")
<xarray.Dataset> Size: 256B
Dimensions:  (lat: 3, lon: 3, time: 2)
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 24B 100.0 120.0 150.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 72B 1.0 2.0 nan 3.0 5.0 nan nan nan nan
    var2     (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0
    var3     (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0
```

```pycon
>>> xr.merge([x, y, z], compat="equals", join="outer", fill_value=-999.0)
<xarray.Dataset> Size: 256B
Dimensions:  (lat: 3, lon: 3, time: 2)
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 24B 100.0 120.0 150.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 72B 1.0 2.0 -999.0 3.0 ... -999.0 -999.0 -999.0
    var2     (lat, lon) float64 72B 5.0 -999.0 6.0 -999.0 ... 7.0 -999.0 8.0
    var3     (time, lon) float64 48B 0.0 -999.0 3.0 4.0 -999.0 9.0
```

```pycon
>>> xr.merge([x, y, z], join="override")
<xarray.Dataset> Size: 144B
Dimensions:  (lat: 2, lon: 2, time: 2)
Coordinates:
  * lat      (lat) float64 16B 35.0 40.0
  * lon      (lon) float64 16B 100.0 120.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 32B 1.0 2.0 3.0 5.0
    var2     (lat, lon) float64 32B 5.0 6.0 7.0 8.0
    var3     (time, lon) float64 32B 0.0 3.0 4.0 9.0
```

```pycon
>>> xr.merge([x, y, z], join="inner")
<xarray.Dataset> Size: 64B
Dimensions:  (lat: 1, lon: 1, time: 2)
Coordinates:
  * lat      (lat) float64 8B 35.0
  * lon      (lon) float64 8B 100.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 8B 1.0
    var2     (lat, lon) float64 8B 5.0
    var3     (time, lon) float64 16B 0.0 4.0
```

```pycon
>>> xr.merge([x, y, z], compat="identical", join="inner")
<xarray.Dataset> Size: 64B
Dimensions:  (lat: 1, lon: 1, time: 2)
Coordinates:
  * lat      (lat) float64 8B 35.0
  * lon      (lon) float64 8B 100.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 8B 1.0
    var2     (lat, lon) float64 8B 5.0
    var3     (time, lon) float64 16B 0.0 4.0
```

```pycon
>>> xr.merge([x, y, z], compat="broadcast_equals", join="outer")
<xarray.Dataset> Size: 256B
Dimensions:  (lat: 3, lon: 3, time: 2)
Coordinates:
  * lat      (lat) float64 24B 35.0 40.0 42.0
  * lon      (lon) float64 24B 100.0 120.0 150.0
  * time     (time) float64 16B 30.0 60.0
Data variables:
    var1     (lat, lon) float64 72B 1.0 2.0 nan 3.0 5.0 nan nan nan nan
    var2     (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0
    var3     (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0
```

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

<div id="2d951b56-ff3e-4fa5-b40a-2c8084a2650e" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('22210e06-b656-4626-a896-16ce389049c6','2d951b56-ff3e-4fa5-b40a-2c8084a2650e')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('22210e06-b656-4626-a896-16ce389049c6','2d951b56-ff3e-4fa5-b40a-2c8084a2650e')">Open In Tab</button></div><div id="ad1ad89d-f43a-4b79-8171-0adaf017aae9" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
* **Raises:**
  [**xarray.MergeError**](xarray.MergeError.html.md#xarray.MergeError) – If any variables with the same name have conflicting values.

#### SEE ALSO
[`concat`](xarray.concat.html.md#xarray.concat), [`combine_nested`](xarray.combine_nested.html.md#xarray.combine_nested), [`combine_by_coords`](xarray.combine_by_coords.html.md#xarray.combine_by_coords)
