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

Merged
merged 22 commits into from
Jan 30, 2025
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
use groupby_defaultdict
  • Loading branch information
kmuehlbauer committed Jun 6, 2024
commit 8202420cc9d66bb4894367e11ed536a63ec3c1c3
13 changes: 11 additions & 2 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):
kmuehlbauer marked this conversation as resolved.
Show resolved Hide resolved
"""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,7 +959,7 @@ def combine_by_coords(
]

# Group by data vars
grouped_by_vars = itertools.groupby(data_objects, 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
Loading