From b3a35beee99996b116f30fb9c6c02f1ed556c262 Mon Sep 17 00:00:00 2001 From: Niclas Rieger Date: Thu, 15 Aug 2024 23:53:26 +0200 Subject: [PATCH 1/3] fix GH9201 Provide a more informative error message, specifically which coordinate is without index. --- xarray/core/combine.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 6f652cb6597..4b4a07ddc77 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -89,10 +89,12 @@ def _infer_concat_order_from_coords(datasets): # Need to read coordinate values to do ordering indexes = [ds._indexes.get(dim) for ds in datasets] if any(index is None for index in indexes): - raise ValueError( - "Every dimension needs a coordinate for " - "inferring concatenation order" + error_msg = ( + f"Every dimension requires a corresponding 1D coordinate " + f"and index for inferring concatenation order but the " + f"coordinate '{dim}' has no corresponding index" ) + raise ValueError(error_msg) # TODO (benbovy, flexible indexes): support flexible indexes? indexes = [index.to_pandas_index() for index in indexes] From 0d6f210fd23ed2d0bcad079843947fb150554bdd Mon Sep 17 00:00:00 2001 From: Niclas Rieger Date: Thu, 15 Aug 2024 23:54:59 +0200 Subject: [PATCH 2/3] add simple test --- xarray/tests/test_combine.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index aad7103c112..c41a874b049 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -1163,3 +1163,16 @@ def test_combine_by_coords_raises_for_differing_types(): TypeError, match=r"Cannot combine along dimension 'time' with mixed types." ): combine_by_coords([da_1, da_2]) + + +def test_combine_by_coords_raises_for_no_index(): + # previously failed with uninformative ValueError + + da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3]}) + da2 = DataArray([1, 2, 3], dims="x", coords={"x": [4, 5, 6]}) + da1 = da1.drop_indexes("x") + with pytest.raises( + ValueError, + match=r"Every dimension requires a corresponding 1D coordinate and index for inferring concatenation order but the coordinate 'x' has no corresponding index", + ): + combine_by_coords([da1, da2]) From f8c5c112b1e99c0f38a66b958a051d55ba740f5f Mon Sep 17 00:00:00 2001 From: Niclas Rieger Date: Fri, 16 Aug 2024 00:26:09 +0200 Subject: [PATCH 3/3] fix ValueError in combine_by_coords test --- xarray/tests/test_combine.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index c41a874b049..1c48dca825d 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -728,7 +728,10 @@ def test_combine_by_coords(self): combine_by_coords(objs) objs = [Dataset({"x": [0], "y": [0]}), Dataset({"x": [0]})] - with pytest.raises(ValueError, match=r"Every dimension needs a coordinate"): + with pytest.raises( + ValueError, + match=r"Every dimension requires a corresponding 1D coordinate and index", + ): combine_by_coords(objs) def test_empty_input(self): @@ -1163,16 +1166,3 @@ def test_combine_by_coords_raises_for_differing_types(): TypeError, match=r"Cannot combine along dimension 'time' with mixed types." ): combine_by_coords([da_1, da_2]) - - -def test_combine_by_coords_raises_for_no_index(): - # previously failed with uninformative ValueError - - da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3]}) - da2 = DataArray([1, 2, 3], dims="x", coords={"x": [4, 5, 6]}) - da1 = da1.drop_indexes("x") - with pytest.raises( - ValueError, - match=r"Every dimension requires a corresponding 1D coordinate and index for inferring concatenation order but the coordinate 'x' has no corresponding index", - ): - combine_by_coords([da1, da2])