Skip to content

Commit

Permalink
Make _replace more lenient. (#9517)
Browse files Browse the repository at this point in the history
* Make _replace more lenient.

Closes #5361

* review comments
  • Loading branch information
dcherian committed Sep 19, 2024
1 parent 7750c00 commit 3c74509
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,10 @@ def _replace_maybe_drop_dims(
variable: Variable,
name: Hashable | None | Default = _default,
) -> Self:
if variable.dims == self.dims and variable.shape == self.shape:
if self.sizes == variable.sizes:
coords = self._coords.copy()
indexes = self._indexes
elif variable.dims == self.dims:
elif set(self.dims) == set(variable.dims):
# Shape has changed (e.g. from reduce(..., keepdims=True)
new_sizes = dict(zip(self.dims, variable.shape, strict=True))
coords = {
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2919,3 +2919,16 @@ def test_gappy_resample_reductions(reduction):
# 1. lambda x: x
# 2. grouped-reduce on unique coords is identical to array
# 3. group_over == groupby-reduce along other dimensions


def test_groupby_transpose():
# GH5361
data = xr.DataArray(
np.random.randn(4, 2),
dims=["x", "z"],
coords={"x": ["a", "b", "a", "c"], "y": ("x", [0, 1, 0, 2])},
)
first = data.T.groupby("x").sum()
second = data.groupby("x").sum()

assert_identical(first, second.transpose(*first.dims))

0 comments on commit 3c74509

Please sign in to comment.