🍾 Xarray is now 10 years old! 🎉

xarray.NamedArray.broadcast_to

xarray.NamedArray.broadcast_to#

NamedArray.broadcast_to(dim=None, **dim_kwargs)[source]#

Broadcast the NamedArray to a new shape. New dimensions are not allowed.

This method allows for the expansion of the array’s dimensions to a specified shape. It handles both positional and keyword arguments for specifying the dimensions to broadcast. An error is raised if new dimensions are attempted to be added.

Parameters:
  • dim (dict, str, sequence of str, optional) – Dimensions to broadcast the array to. If a dict, keys are dimension names and values are the new sizes. If a string or sequence of strings, existing dimensions are matched with a size of 1.

  • **dim_kwargs (Any) – Additional dimensions specified as keyword arguments. Each keyword argument specifies the name of an existing dimension and its size.

Returns:

NamedArray – A new NamedArray with the broadcasted dimensions.

Examples

>>> data = np.asarray([[1.0, 2.0], [3.0, 4.0]])
>>> array = xr.NamedArray(("x", "y"), data)
>>> array.sizes
{'x': 2, 'y': 2}
>>> broadcasted = array.broadcast_to(x=2, y=2)
>>> broadcasted.sizes
{'x': 2, 'y': 2}