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 rai
(
self, dim: Mapping[_Dim, int] | None = None, **dim_kwargs: Any
)
| 1061 | return self.permute_dims() |
| 1062 | |
| 1063 | def broadcast_to( |
| 1064 | self, dim: Mapping[_Dim, int] | None = None, **dim_kwargs: Any |
| 1065 | ) -> NamedArray[Any, _DType_co]: |
| 1066 | """ |
| 1067 | Broadcast the NamedArray to a new shape. New dimensions are not allowed. |
| 1068 | |
| 1069 | This method allows for the expansion of the array's dimensions to a specified shape. |
| 1070 | It handles both positional and keyword arguments for specifying the dimensions to broadcast. |
| 1071 | An error is raised if new dimensions are attempted to be added. |
| 1072 | |
| 1073 | Parameters |
| 1074 | ---------- |
| 1075 | dim : dict, str, sequence of str, optional |
| 1076 | Dimensions to broadcast the array to. If a dict, keys are dimension names and values are the new sizes. |
| 1077 | If a string or sequence of strings, existing dimensions are matched with a size of 1. |
| 1078 | |
| 1079 | **dim_kwargs : Any |
| 1080 | Additional dimensions specified as keyword arguments. Each keyword argument specifies the name of an existing dimension and its size. |
| 1081 | |
| 1082 | Returns |
| 1083 | ------- |
| 1084 | NamedArray |
| 1085 | A new NamedArray with the broadcasted dimensions. |
| 1086 | |
| 1087 | Examples |
| 1088 | -------- |
| 1089 | >>> data = np.asarray([[1.0, 2.0], [3.0, 4.0]]) |
| 1090 | >>> array = xr.NamedArray(("x", "y"), data) |
| 1091 | >>> array.sizes |
| 1092 | {'x': 2, 'y': 2} |
| 1093 | |
| 1094 | >>> broadcasted = array.broadcast_to(x=2, y=2) |
| 1095 | >>> broadcasted.sizes |
| 1096 | {'x': 2, 'y': 2} |
| 1097 | """ |
| 1098 | |
| 1099 | from xarray.core import duck_array_ops |
| 1100 | |
| 1101 | combined_dims = either_dict_or_kwargs(dim, dim_kwargs, "broadcast_to") |
| 1102 | |
| 1103 | # Check that no new dimensions are added |
| 1104 | if new_dims := set(combined_dims) - set(self.dims): |
| 1105 | raise ValueError( |
| 1106 | f"Cannot add new dimensions: {new_dims}. Only existing dimensions are allowed. " |
| 1107 | "Use `expand_dims` method to add new dimensions." |
| 1108 | ) |
| 1109 | |
| 1110 | # Create a dictionary of the current dimensions and their sizes |
| 1111 | current_shape = self.sizes |
| 1112 | |
| 1113 | # Update the current shape with the new dimensions, keeping the order of the original dimensions |
| 1114 | broadcast_shape = {d: current_shape.get(d, 1) for d in self.dims} |
| 1115 | broadcast_shape |= combined_dims |
| 1116 | |
| 1117 | # Ensure the dimensions are in the correct order |
| 1118 | ordered_dims = list(broadcast_shape.keys()) |
| 1119 | ordered_shape = tuple(broadcast_shape[d] for d in ordered_dims) |
| 1120 | data = duck_array_ops.broadcast_to(self._data, ordered_shape) # type: ignore[no-untyped-call] # TODO: use array-api-compat function |