diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 827c0a3588f..77ea1b341c9 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -173,11 +173,12 @@ def _inverse_permutation_indices(positions, N: int | None = None) -> np.ndarray if isinstance(positions[0], slice): positions = _consolidate_slices(positions) - if positions == slice(None): + if positions == [slice(None)] or positions == [slice(0, None)]: return None positions = [np.arange(sl.start, sl.stop, sl.step) for sl in positions] - - newpositions = nputils.inverse_permutation(np.concatenate(positions), N) + newpositions = nputils.inverse_permutation( + np.concatenate(tuple(p for p in positions if len(p) > 0)), N + ) return newpositions[newpositions != -1] @@ -990,7 +991,7 @@ def _maybe_reindex(self, combined): if (has_missing_groups and index is not None) or ( len(self.groupers) > 1 and not isinstance(grouper.full_index, pd.RangeIndex) - and not index.index.equals(grouper.full_index) + and not (index is not None and index.index.equals(grouper.full_index)) ): indexers[grouper.name] = grouper.full_index if indexers: diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 47ea2fcd2b0..5075fa12b92 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -3850,6 +3850,20 @@ def test_groupby_bins_mean_time_series(): assert ds_agged.time.dtype == np.dtype("datetime64[ns]") +def test_groupby_multi_map(): + # https://github.com/pydata/xarray/issues/11004 + d = xr.DataArray( + [[0, 1], [2, 3]], + coords={ + "lon": (["ny", "nx"], [[30, 40], [40, 50]]), + "lat": (["ny", "nx"], [[10, 10], [20, 20]]), + }, + dims=["ny", "nx"], + ) + xr.testing.assert_equal(d, d.groupby("lon").map(lambda x: x)) + xr.testing.assert_equal(d, d.groupby(("lon", "lat")).map(lambda x: x)) + + # TODO: Possible property tests to add to this module # 1. lambda x: x # 2. grouped-reduce on unique coords is identical to array