MCPcopy
hub / github.com/pydata/xarray / _resolve_group

Function _resolve_group

xarray/core/groupby.py:459–509  ·  view source on GitHub ↗
(
    obj: T_DataWithCoords, group: T_Group | Hashable | IndexVariable
)

Source from the content-addressed store, hash-verified

457
458
459def _resolve_group(
460 obj: T_DataWithCoords, group: T_Group | Hashable | IndexVariable
461) -> T_Group:
462 from xarray.core.dataarray import DataArray
463
464 error_msg = (
465 "the group variable's length does not "
466 "match the length of this variable along its "
467 "dimensions"
468 )
469
470 newgroup: T_Group
471 if isinstance(group, DataArray):
472 try:
473 align(obj, group, join="exact", copy=False)
474 except ValueError as err:
475 raise ValueError(error_msg) from err
476
477 newgroup = group.copy(deep=False)
478 newgroup.name = group.name or "group"
479
480 elif isinstance(group, IndexVariable):
481 # This assumption is built in to _ensure_1d.
482 if group.ndim != 1:
483 raise ValueError(
484 "Grouping by multi-dimensional IndexVariables is not allowed."
485 "Convert to and pass a DataArray instead."
486 )
487 (group_dim,) = group.dims
488 if len(group) != obj.sizes[group_dim]:
489 raise ValueError(error_msg)
490 newgroup = DataArray(group)
491
492 else:
493 if not hashable(group):
494 raise TypeError(
495 "`group` must be an xarray.DataArray or the "
496 "name of an xarray variable or dimension. "
497 f"Received {group!r} instead."
498 )
499 group_da: DataArray = obj[group]
500 if group_da.name not in obj._indexes and group_da.name in obj.dims:
501 # DummyGroups should not appear on groupby results
502 newgroup = _DummyGroup(obj, group_da.name, group_da.coords)
503 else:
504 newgroup = group_da
505
506 if newgroup.size == 0:
507 raise ValueError(f"{newgroup.name} must not be empty")
508
509 return newgroup
510
511
512@dataclass

Callers 1

__post_init__Method · 0.85

Calls 5

alignFunction · 0.90
DataArrayClass · 0.90
hashableFunction · 0.90
_DummyGroupClass · 0.85
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…