xarray.combine_by_coords(datasets, compat='no_conflicts', data_vars='all', coords='different', fill_value=<NA>, join='outer')

Attempt to auto-magically combine the given datasets into one by using dimension coordinates.

This method attempts to combine a group of datasets along any number of dimensions into a single entity by inspecting coords and metadata and using a combination of concat and merge.

Will attempt to order the datasets such that the values in their dimension coordinates are monotonic along all dimensions. If it cannot determine the order in which to concatenate the datasets, it will raise a ValueError. Non-coordinate dimensions will be ignored, as will any coordinate dimensions which do not vary between each dataset.

Aligns coordinates, but different variables on datasets can cause it to fail under some scenarios. In complex cases, you may need to clean up your data and use concat/merge explicitly (also see manual_combine).

Works well if, for example, you have N years of data and M data variables, and each combination of a distinct time period and set of data variables is saved as its own dataset. Also useful for if you have a simulation which is parallelized in multiple dimensions, but has global coordinates saved in each file specifying the positions of points within the global domain.

  • datasets (sequence of xarray.Dataset) – Dataset objects to combine.

  • compat ({'identical', 'equals', 'broadcast_equals',) –

    ‘no_conflicts’, ‘override’}, optional String indicating how to compare variables of the same name for potential conflicts:

    • ’broadcast_equals’: all values must be equal when variables are broadcast against each other to ensure common dimensions.

    • ’equals’: all values and dimensions must be the same.

    • ’identical’: all values, dimensions and attributes must be the same.

    • ’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

  • data_vars ({'minimal', 'different', 'all' or list of str}, optional) – Details are in the documentation of concat

  • coords ({'minimal', 'different', 'all' or list of str}, optional) – Details are in the documentation of concat

  • fill_value (scalar, optional) – Value to use for newly missing values

  • join ({'outer', 'inner', 'left', 'right', 'exact'}, optional) –

    String indicating how to combine differing indexes (excluding concat_dim) 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.



Return type



Combining two datasets using their common dimension coordinates. Notice they are concatenated based on the values in their dimension coordinates, not on their position in the list passed to combine_by_coords.

>>> x1
Dimensions:         (x: 3)
  * position        (x) int64   0 1 2
Data variables:
    temperature     (x) float64 11.04 23.57 20.77 ...
>>> x2
Dimensions:         (x: 3)
  * position        (x) int64   3 4 5
Data variables:
    temperature     (x) float64 6.97 8.13 7.42 ...
>>> combined = xr.combine_by_coords([x2, x1])
Dimensions:         (x: 6)
  * position        (x) int64   0 1 2 3 4 5
Data variables:
    temperature     (x) float64 11.04 23.57 20.77 ...