Skip to content

Commit 3ef75ae

Browse files
authored
concat now handles non-dim coordinates only present in one dataset (#3769)
* concat can now deal with non-dim coordinates only present in one dataset. * fix test * minor fixes.
1 parent 24cfdd2 commit 3ef75ae

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

doc/whats-new.rst

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Bug fixes
3838
checking. (:issue:`3779`, :pull:`3787`)
3939
By `Justus Magin <https://github.com/keewis>`_.
4040

41+
- :py:func:`concat` can now handle coordinate variables only present in one of
42+
the objects to be concatenated when ``coords="different"``.
43+
By `Deepak Cherian <https://github.com/dcherian>`_.
44+
4145
Documentation
4246
~~~~~~~~~~~~~
4347

xarray/core/concat.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,23 @@ def process_subset_opt(opt, subset):
194194
for k in getattr(datasets[0], subset):
195195
if k not in concat_over:
196196
equals[k] = None
197-
variables = [ds.variables[k] for ds in datasets]
197+
198+
variables = []
199+
for ds in datasets:
200+
if k in ds.variables:
201+
variables.append(ds.variables[k])
202+
203+
if len(variables) == 1:
204+
# coords="different" doesn't make sense when only one object
205+
# contains a particular variable.
206+
break
207+
elif len(variables) != len(datasets) and opt == "different":
208+
raise ValueError(
209+
f"{k!r} not present in all datasets and coords='different'. "
210+
f"Either add {k!r} to datasets where it is missing or "
211+
"specify coords='minimal'."
212+
)
213+
198214
# first check without comparing values i.e. no computes
199215
for var in variables[1:]:
200216
equals[k] = getattr(variables[0], compat)(

xarray/tests/test_combine.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@ def test_nested_concat(self):
365365
expected = Dataset({"x": ("a", [0, 1]), "y": ("a", [0, 1])})
366366
assert_identical(expected, actual)
367367

368-
objs = [Dataset({"x": [0], "y": [0]}), Dataset({"x": [0]})]
369-
with pytest.raises(KeyError):
370-
combine_nested(objs, concat_dim="x")
368+
objs = [Dataset({"x": [0], "y": [0]}), Dataset({"x": [1]})]
369+
actual = combine_nested(objs, concat_dim="x")
370+
expected = Dataset({"x": [0, 1], "y": [0]})
371+
assert_identical(expected, actual)
371372

372373
@pytest.mark.parametrize(
373374
"join, expected",

xarray/tests/test_concat.py

+21
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,24 @@ def test_concat_attrs_first_variable(attr1, attr2):
475475

476476
concat_attrs = concat(arrs, "y").attrs
477477
assert concat_attrs == attr1
478+
479+
480+
def test_concat_merge_single_non_dim_coord():
481+
da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3], "y": 1})
482+
da2 = DataArray([4, 5, 6], dims="x", coords={"x": [4, 5, 6]})
483+
484+
expected = DataArray(range(1, 7), dims="x", coords={"x": range(1, 7), "y": 1})
485+
486+
for coords in ["different", "minimal"]:
487+
actual = concat([da1, da2], "x", coords=coords)
488+
assert_identical(actual, expected)
489+
490+
with raises_regex(ValueError, "'y' is not present in all datasets."):
491+
concat([da1, da2], dim="x", coords="all")
492+
493+
da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3], "y": 1})
494+
da2 = DataArray([4, 5, 6], dims="x", coords={"x": [4, 5, 6]})
495+
da3 = DataArray([7, 8, 9], dims="x", coords={"x": [7, 8, 9], "y": 1})
496+
for coords in ["different", "all"]:
497+
with raises_regex(ValueError, "'y' not present in all datasets"):
498+
concat([da1, da2, da3], dim="x")

0 commit comments

Comments
 (0)