xugrid.merge#

xugrid.merge(objects: Iterable[DataArray | CoercibleMapping], compat: CompatOptions = 'no_conflicts', join: JoinOptions = 'outer', fill_value: object = <NA>, combine_attrs: CombineAttrsOptions = 'override') Dataset[source]#

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

Parameters:
  • objects (iterable of Dataset or iterable of DataArray or iterable of dict-like) – 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 or dict-like, 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, 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 with combined variables from each object.

Return type:

Dataset

Examples

>>> 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",
... )
>>> 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
>>> 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
>>> 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
>>> xr.merge([x, y, z])
<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
>>> xr.merge([x, y, z], compat="identical")
<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
>>> xr.merge([x, y, z], compat="equals")
<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
>>> xr.merge([x, y, z], compat="equals", 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> xr.merge([x, y, z], join="exact")
Traceback (most recent call last):
...
ValueError: cannot align objects with join='exact' where ...
Raises:

xarray.MergeError – If any variables with the same name have conflicting values.

See also

concat, combine_nested, combine_by_coords