Skip to content

Preserve label ordering for multi-variable GroupBy #10151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ def factorize(self) -> EncodedGroups:
list(grouper.full_index.values for grouper in groupers),
names=tuple(grouper.name for grouper in groupers),
)
if not full_index.is_unique:
raise ValueError(
"The output index for the GroupBy is non-unique. "
"This is a bug in the Grouper provided."
)
# This will be unused when grouping by dask arrays, so skip..
if not is_chunked_array(_flatcodes):
# Constructing an index from the product is wrong when there are missing groups
Expand Down Expand Up @@ -942,17 +947,29 @@ def _binary_op(self, other, f, reflexive=False):
def _restore_dim_order(self, stacked):
raise NotImplementedError

def _maybe_restore_empty_groups(self, combined):
"""Our index contained empty groups (e.g., from a resampling or binning). If we
def _maybe_reindex(self, combined):
"""Reindexing is needed in two cases:
1. Our index contained empty groups (e.g., from a resampling or binning). If we
reduced on that dimension, we want to restore the full index.

2. We use a MultiIndex for multi-variable GroupBy.
The MultiIndex stores each level's labels in sorted order
which are then assigned on unstacking. So we need to restore
the correct order here.
"""
has_missing_groups = (
self.encoded.unique_coord.size != self.encoded.full_index.size
)
indexers = {}
for grouper in self.groupers:
if has_missing_groups and grouper.name in combined._indexes:
index = combined._indexes.get(grouper.name, None)
if has_missing_groups and index is not None:
indexers[grouper.name] = grouper.full_index
elif len(self.groupers) > 1:
if not isinstance(
grouper.full_index, pd.RangeIndex
) and not index.index.equals(grouper.full_index):
indexers[grouper.name] = grouper.full_index
if indexers:
combined = combined.reindex(**indexers)
return combined
Expand Down Expand Up @@ -1540,7 +1557,7 @@ def _combine(self, applied, shortcut=False):
if dim not in applied_example.dims:
combined = combined.assign_coords(self.encoded.coords)
combined = self._maybe_unstack(combined)
combined = self._maybe_restore_empty_groups(combined)
combined = self._maybe_reindex(combined)
return combined

def reduce(
Expand Down Expand Up @@ -1696,7 +1713,7 @@ def _combine(self, applied):
if dim not in applied_example.dims:
combined = combined.assign_coords(self.encoded.coords)
combined = self._maybe_unstack(combined)
combined = self._maybe_restore_empty_groups(combined)
combined = self._maybe_reindex(combined)
return combined

def reduce(
Expand Down
2 changes: 1 addition & 1 deletion xarray/groupers.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def first_items(self) -> tuple[pd.Series, np.ndarray]:
counts = grouped.count()
# This way we generate codes for the final output index: full_index.
# So for _flox_reduce we avoid one reindex and copy by avoiding
# _maybe_restore_empty_groups
# _maybe_reindex
codes = np.repeat(np.arange(len(first_items)), counts)
return first_items, codes

Expand Down
47 changes: 38 additions & 9 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_multi_index_groupby_sum() -> None:


@requires_pandas_ge_2_2
def test_multi_index_propagation():
def test_multi_index_propagation() -> None:
# regression test for GH9648
times = pd.date_range("2023-01-01", periods=4)
locations = ["A", "B"]
Expand Down Expand Up @@ -2289,7 +2289,7 @@ def test_resample_origin(self) -> None:
times = pd.date_range("2000-01-01T02:03:01", freq="6h", periods=10)
array = DataArray(np.arange(10), [("time", times)])

origin = "start"
origin: Literal["start"] = "start"
actual = array.resample(time="24h", origin=origin).mean()
expected = DataArray(array.to_series().resample("24h", origin=origin).mean())
assert_identical(expected, actual)
Expand Down Expand Up @@ -2694,7 +2694,7 @@ def test_default_flox_method() -> None:

@requires_cftime
@pytest.mark.filterwarnings("ignore")
def test_cftime_resample_gh_9108():
def test_cftime_resample_gh_9108() -> None:
import cftime

ds = Dataset(
Expand Down Expand Up @@ -3044,7 +3044,7 @@ def test_gappy_resample_reductions(reduction):
assert_identical(expected, actual)


def test_groupby_transpose():
def test_groupby_transpose() -> None:
# GH5361
data = xr.DataArray(
np.random.randn(4, 2),
Expand Down Expand Up @@ -3104,7 +3104,7 @@ def test_lazy_grouping(grouper, expect_index):


@requires_dask
def test_lazy_grouping_errors():
def test_lazy_grouping_errors() -> None:
import dask.array

data = DataArray(
Expand All @@ -3130,15 +3130,15 @@ def test_lazy_grouping_errors():


@requires_dask
def test_lazy_int_bins_error():
def test_lazy_int_bins_error() -> None:
import dask.array

with pytest.raises(ValueError, match="Bin edges must be provided"):
with raise_if_dask_computes():
_ = BinGrouper(bins=4).factorize(DataArray(dask.array.arange(3)))


def test_time_grouping_seasons_specified():
def test_time_grouping_seasons_specified() -> None:
time = xr.date_range("2001-01-01", "2002-01-01", freq="D")
ds = xr.Dataset({"foo": np.arange(time.size)}, coords={"time": ("time", time)})
labels = ["DJF", "MAM", "JJA", "SON"]
Expand All @@ -3147,7 +3147,36 @@ def test_time_grouping_seasons_specified():
assert_identical(actual, expected.reindex(season=labels))


def test_groupby_multiple_bin_grouper_missing_groups():
def test_multiple_grouper_unsorted_order() -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is hard to review, but I would appreciate someone taking a look at this test.

time = xr.date_range("2001-01-01", "2003-01-01", freq="MS")
ds = xr.Dataset({"foo": np.arange(time.size)}, coords={"time": ("time", time)})
labels = ["DJF", "MAM", "JJA", "SON"]
actual = ds.groupby(
{
"time.season": UniqueGrouper(labels=labels),
"time.year": UniqueGrouper(labels=[2002, 2001]),
}
).sum()
expected = (
ds.groupby({"time.season": UniqueGrouper(), "time.year": UniqueGrouper()})
.sum()
.reindex(season=labels, year=[2002, 2001])
)
assert_identical(actual, expected.reindex(season=labels))

b = xr.DataArray(
np.random.default_rng(0).random((2, 3, 4)),
coords={"x": [0, 1], "y": [0, 1, 2]},
dims=["x", "y", "z"],
)
actual2 = b.groupby(
x=UniqueGrouper(labels=[1, 0]), y=UniqueGrouper(labels=[2, 0, 1])
).sum()
expected2 = b.reindex(x=[1, 0], y=[2, 0, 1]).transpose("z", ...)
assert_identical(actual2, expected2)


def test_groupby_multiple_bin_grouper_missing_groups() -> None:
from numpy import nan

ds = xr.Dataset(
Expand Down Expand Up @@ -3224,7 +3253,7 @@ def test_shuffle_by(chunks, expected_chunks):


@requires_dask
def test_groupby_dask_eager_load_warnings():
def test_groupby_dask_eager_load_warnings() -> None:
ds = xr.Dataset(
{"foo": (("z"), np.arange(12))},
coords={"x": ("z", np.arange(12)), "y": ("z", np.arange(12))},
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy