(
obj: T_Xarray,
group: GroupInput,
groupers: dict[str, Grouper],
*,
eagerly_compute_group: Literal[False] | None,
)
| 385 | |
| 386 | |
| 387 | def _parse_group_and_groupers( |
| 388 | obj: T_Xarray, |
| 389 | group: GroupInput, |
| 390 | groupers: dict[str, Grouper], |
| 391 | *, |
| 392 | eagerly_compute_group: Literal[False] | None, |
| 393 | ) -> tuple[ResolvedGrouper, ...]: |
| 394 | from xarray.core.dataarray import DataArray |
| 395 | from xarray.groupers import Grouper, UniqueGrouper |
| 396 | |
| 397 | if group is not None and groupers: |
| 398 | raise ValueError( |
| 399 | "Providing a combination of `group` and **groupers is not supported." |
| 400 | ) |
| 401 | |
| 402 | if group is None and not groupers: |
| 403 | raise ValueError("Either `group` or `**groupers` must be provided.") |
| 404 | |
| 405 | if isinstance(group, np.ndarray | pd.Index): |
| 406 | raise TypeError( |
| 407 | f"`group` must be a DataArray. Received {type(group).__name__!r} instead" |
| 408 | ) |
| 409 | |
| 410 | if isinstance(group, Grouper): |
| 411 | raise TypeError( |
| 412 | "Cannot group by a Grouper object. " |
| 413 | f"Instead use `.groupby(var_name={type(group).__name__}(...))`. " |
| 414 | "You may need to assign the variable you're grouping by as a coordinate using `assign_coords`." |
| 415 | ) |
| 416 | |
| 417 | if isinstance(group, Mapping): |
| 418 | grouper_mapping = either_dict_or_kwargs(group, groupers, "groupby") |
| 419 | group = None |
| 420 | |
| 421 | rgroupers: tuple[ResolvedGrouper, ...] |
| 422 | if isinstance(group, DataArray | Variable): |
| 423 | rgroupers = ( |
| 424 | ResolvedGrouper( |
| 425 | UniqueGrouper(), group, obj, eagerly_compute_group=eagerly_compute_group |
| 426 | ), |
| 427 | ) |
| 428 | else: |
| 429 | if group is not None: |
| 430 | if TYPE_CHECKING: |
| 431 | assert isinstance(group, str | Sequence) |
| 432 | group_iter: Sequence[Hashable] = ( |
| 433 | (group,) if isinstance(group, str) else group |
| 434 | ) |
| 435 | grouper_mapping = {g: UniqueGrouper() for g in group_iter} |
| 436 | elif groupers: |
| 437 | grouper_mapping = cast("Mapping[Hashable, Grouper]", groupers) |
| 438 | |
| 439 | rgroupers = tuple( |
| 440 | ResolvedGrouper( |
| 441 | grouper, group, obj, eagerly_compute_group=eagerly_compute_group |
| 442 | ) |
| 443 | for group, grouper in grouper_mapping.items() |
| 444 | ) |
no test coverage detected
searching dependent graphs…