Skip to content

Commit 5693ac7

Browse files
authored
Improve error message for missing coordinate index (#9370)
* fix GH9201 Provide a more informative error message, specifically which coordinate is without index. * add simple test * fix ValueError in combine_by_coords test
1 parent 619cfb5 commit 5693ac7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

xarray/core/combine.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ def _infer_concat_order_from_coords(datasets):
8989
# Need to read coordinate values to do ordering
9090
indexes = [ds._indexes.get(dim) for ds in datasets]
9191
if any(index is None for index in indexes):
92-
raise ValueError(
93-
"Every dimension needs a coordinate for "
94-
"inferring concatenation order"
92+
error_msg = (
93+
f"Every dimension requires a corresponding 1D coordinate "
94+
f"and index for inferring concatenation order but the "
95+
f"coordinate '{dim}' has no corresponding index"
9596
)
97+
raise ValueError(error_msg)
9698

9799
# TODO (benbovy, flexible indexes): support flexible indexes?
98100
indexes = [index.to_pandas_index() for index in indexes]

xarray/tests/test_combine.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,10 @@ def test_combine_by_coords(self):
728728
combine_by_coords(objs)
729729

730730
objs = [Dataset({"x": [0], "y": [0]}), Dataset({"x": [0]})]
731-
with pytest.raises(ValueError, match=r"Every dimension needs a coordinate"):
731+
with pytest.raises(
732+
ValueError,
733+
match=r"Every dimension requires a corresponding 1D coordinate and index",
734+
):
732735
combine_by_coords(objs)
733736

734737
def test_empty_input(self):

0 commit comments

Comments
 (0)