xarray.align (original) (raw)

xarray.align(*objects, join='inner', copy=True, indexes=None, exclude=frozenset({}), fill_value=)[source]#

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:

Returns:

aligned (tuple of DataArray or Dataset) – Tuple of objects with the same type as *objects with aligned coordinates.

Raises:

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

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]}, ... )

x <xarray.DataArray (lat: 2, lon: 2)> Size: 32B array([[25, 35], [10, 24]]) Coordinates:

y <xarray.DataArray (lat: 2, lon: 2)> Size: 32B array([[20, 5], [ 7, 13]]) Coordinates:

a, b = xr.align(x, y) a <xarray.DataArray (lat: 1, lon: 2)> Size: 16B array([[25, 35]]) Coordinates:

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:

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:

a, b = xr.align(x, y, join="left") a <xarray.DataArray (lat: 2, lon: 2)> Size: 32B array([[25, 35], [10, 24]]) Coordinates:

a, b = xr.align(x, y, join="right") a <xarray.DataArray (lat: 2, lon: 2)> Size: 32B array([[25., 35.], [nan, nan]]) Coordinates:

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

a, b = xr.align(x, y, join="override") a <xarray.DataArray (lat: 2, lon: 2)> Size: 32B array([[25, 35], [10, 24]]) Coordinates: