Skip to content

Commit

Permalink
Cleanup + fix typnig
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Mar 9, 2023
1 parent e798786 commit f30da34
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_reduce_dims(reduce_dims, dimensions):

def unique_value_groups(
ar, sort: bool = True
) -> tuple[np.ndarray | pd.Index, list[list[int]]]:
) -> tuple[np.ndarray | pd.Index, list[list[int]], np.ndarray]:
"""Group an array by its unique values.
Parameters
Expand Down Expand Up @@ -706,6 +706,7 @@ def _flox_reduce(
from xarray.core.dataset import Dataset

obj = self._original_obj
group = self._original_group

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)
Expand Down Expand Up @@ -735,32 +736,23 @@ def _flox_reduce(
if index.is_unique and self._squeeze:
raise ValueError(f"cannot reduce over dimensions {self._group.name!r}")

if isinstance(self._original_group, _DummyGroup):
group = self._original_group.name
else:
group = self._original_group

unindexed_dims: tuple[str, ...] = tuple()
if isinstance(group, str):
if group in obj.dims and group not in obj._indexes and self._bins is None:
unindexed_dims = (group,)
group = self._original_obj[group]
unindexed_dims: tuple[Hashable, ...] = tuple()
if isinstance(group, _DummyGroup) and self._bins is None:
unindexed_dims = (group.name,)

parsed_dim: tuple[Hashable, ...]
if isinstance(dim, str):
parsed_dim = (dim,)
elif dim is None:
parsed_dim = group.dims
elif dim is ...:
parsed_dim = tuple(self._original_obj.dims)
parsed_dim = tuple(obj.dims)
else:
parsed_dim = tuple(dim)

# Do this so we raise the same error message whether flox is present or not.
# Better to control it here than in flox.
if any(
d not in group.dims and d not in self._original_obj.dims for d in parsed_dim
):
if any(d not in group.dims and d not in obj.dims for d in parsed_dim):
raise ValueError(f"cannot reduce over dimensions {dim}.")

if kwargs["func"] not in ["all", "any", "count"]:
Expand Down

0 comments on commit f30da34

Please sign in to comment.