MCPcopy Index your code
hub / github.com/pydata/xarray / _ensure_1d

Function _ensure_1d

xarray/core/groupby.py:257–285  ·  view source on GitHub ↗
(
    group: T_Group, obj: T_DataWithCoords
)

Source from the content-addressed store, hash-verified

255
256
257def _ensure_1d(
258 group: T_Group, obj: T_DataWithCoords
259) -> tuple[
260 T_Group,
261 T_DataWithCoords,
262 Hashable | None,
263 list[Hashable],
264]:
265 # 1D cases: do nothing
266 if isinstance(group, _DummyGroup) or group.ndim == 1:
267 return group, obj, None, []
268
269 from xarray.core.dataarray import DataArray
270
271 if isinstance(group, DataArray):
272 for dim in set(group.dims) - set(obj.dims):
273 obj = obj.expand_dims(dim)
274 # try to stack the dims of the group into a single dim
275 orig_dims = group.dims
276 stacked_dim = "stacked_" + "_".join(map(str, orig_dims))
277 # these dimensions get created by the stack operation
278 inserted_dims = [dim for dim in group.dims if dim not in group.coords]
279 # `newgroup` construction is optimized so we don't create an index unnecessarily,
280 # or stack any non-dim coords unnecessarily
281 newgroup = DataArray(group.variable.stack({stacked_dim: orig_dims}))
282 newobj = obj.stack({stacked_dim: orig_dims})
283 return newgroup, newobj, stacked_dim, inserted_dims
284
285 raise TypeError(f"group must be DataArray or _DummyGroup, got {type(group)!r}.")
286
287
288@dataclass

Callers 1

__init__Method · 0.85

Calls 5

DataArrayClass · 0.90
typeFunction · 0.85
expand_dimsMethod · 0.45
joinMethod · 0.45
stackMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…