Skip to content
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

Make _replace more lenient. #9517

Merged
merged 2 commits into from
Sep 19, 2024
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
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 @@ -2918,3 +2918,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))
Loading