๐Ÿพ Xarray is now 10 years old! ๐ŸŽ‰

xarray.DataArray.broadcast_like

xarray.DataArray.broadcast_like#

DataArray.broadcast_like(other, *, exclude=None)[source]#

Broadcast this DataArray against another Dataset or DataArray.

This is equivalent to xr.broadcast(other, self)[1]

xarray objects are broadcast against each other in arithmetic operations, so this method is not be necessary for most uses.

If no change is needed, the input data is returned to the output without being copied.

If new coords are added by the broadcast, their values are NaN filled.

Parameters:
  • other (Dataset or DataArray) โ€“ Object against which to broadcast this array.

  • exclude (iterable of Hashable, optional) โ€“ Dimensions that must not be broadcasted

Returns:

new_da (DataArray) โ€“ The caller broadcasted against other.

Examples

>>> arr1 = xr.DataArray(
...     np.random.randn(2, 3),
...     dims=("x", "y"),
...     coords={"x": ["a", "b"], "y": ["a", "b", "c"]},
... )
>>> arr2 = xr.DataArray(
...     np.random.randn(3, 2),
...     dims=("x", "y"),
...     coords={"x": ["a", "b", "c"], "y": ["a", "b"]},
... )
>>> arr1
<xarray.DataArray (x: 2, y: 3)> Size: 48B
array([[ 1.76405235,  0.40015721,  0.97873798],
       [ 2.2408932 ,  1.86755799, -0.97727788]])
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) <U1 12B 'a' 'b' 'c'
>>> arr2
<xarray.DataArray (x: 3, y: 2)> Size: 48B
array([[ 0.95008842, -0.15135721],
       [-0.10321885,  0.4105985 ],
       [ 0.14404357,  1.45427351]])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'
  * y        (y) <U1 8B 'a' 'b'
>>> arr1.broadcast_like(arr2)
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[ 1.76405235,  0.40015721,  0.97873798],
       [ 2.2408932 ,  1.86755799, -0.97727788],
       [        nan,         nan,         nan]])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'
  * y        (y) <U1 12B 'a' 'b' 'c'