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

Preserve order of variables in combine_by_coords #9070

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- Preserve order of variables in :py:func:`xarray.combine_by_coords` (:issue:`8828`, :pull:`9070`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
14 changes: 11 additions & 3 deletions xarray/core/combine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import itertools
from collections import Counter
from collections import Counter, defaultdict
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Literal, Union

Expand Down Expand Up @@ -591,6 +591,15 @@ def vars_as_keys(ds):
return tuple(sorted(ds))


def groupby_defaultdict(iter, key=lambda x: x):
"""replacement for itertools.groupby"""
idx = defaultdict(list)
for i, obj in enumerate(iter):
idx[key(obj)].append(i)
for k, ix in idx.items():
yield k, (iter[i] for i in ix)


def _combine_single_variable_hypercube(
datasets,
fill_value=dtypes.NA,
Expand Down Expand Up @@ -950,8 +959,7 @@ def combine_by_coords(
]

# Group by data vars
sorted_datasets = sorted(data_objects, key=vars_as_keys)
grouped_by_vars = itertools.groupby(sorted_datasets, key=vars_as_keys)
grouped_by_vars = groupby_defaultdict(data_objects, key=vars_as_keys)

# Perform the multidimensional combine on each group of data variables
# before merging back together
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,20 @@ def test_combine_by_coords_incomplete_hypercube(self):
with pytest.raises(ValueError):
combine_by_coords([x1, x2, x3], fill_value=None)

def test_combine_by_coords_override_order(self):
# regression test for https://github.com/pydata/xarray/issues/8828
x1 = Dataset({"a": (("y", "x"), [[1]])}, coords={"y": [0], "x": [0]})
x2 = Dataset(
{"a": (("y", "x"), [[2]]), "b": (("y", "x"), [[1]])},
coords={"y": [0], "x": [0]},
)
actual = combine_by_coords([x1, x2], compat="override")
assert_equal(actual["a"], actual["b"])
assert_equal(actual["a"], x1["a"])

actual = combine_by_coords([x2, x1], compat="override")
assert_equal(actual["a"], x2["a"])


class TestCombineMixedObjectsbyCoords:
def test_combine_by_coords_mixed_unnamed_dataarrays(self):
Expand Down
Loading