Skip to content

Commit 3c74509

Browse files
authored
Make _replace more lenient. (#9517)
* Make _replace more lenient. Closes #5361 * review comments
1 parent 7750c00 commit 3c74509

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

xarray/core/dataarray.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,10 @@ def _replace_maybe_drop_dims(
534534
variable: Variable,
535535
name: Hashable | None | Default = _default,
536536
) -> Self:
537-
if variable.dims == self.dims and variable.shape == self.shape:
537+
if self.sizes == variable.sizes:
538538
coords = self._coords.copy()
539539
indexes = self._indexes
540-
elif variable.dims == self.dims:
540+
elif set(self.dims) == set(variable.dims):
541541
# Shape has changed (e.g. from reduce(..., keepdims=True)
542542
new_sizes = dict(zip(self.dims, variable.shape, strict=True))
543543
coords = {

xarray/tests/test_groupby.py

+13
Original file line numberDiff line numberDiff line change
@@ -2919,3 +2919,16 @@ def test_gappy_resample_reductions(reduction):
29192919
# 1. lambda x: x
29202920
# 2. grouped-reduce on unique coords is identical to array
29212921
# 3. group_over == groupby-reduce along other dimensions
2922+
2923+
2924+
def test_groupby_transpose():
2925+
# GH5361
2926+
data = xr.DataArray(
2927+
np.random.randn(4, 2),
2928+
dims=["x", "z"],
2929+
coords={"x": ["a", "b", "a", "c"], "y": ("x", [0, 1, 0, 2])},
2930+
)
2931+
first = data.T.groupby("x").sum()
2932+
second = data.groupby("x").sum()
2933+
2934+
assert_identical(first, second.transpose(*first.dims))

0 commit comments

Comments
 (0)