diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 5b73059b34c..81206cc5cc1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -67,6 +67,8 @@ Bug fixes - Line plots with the ``x`` or ``y`` argument set to a 1D non-dimensional coord now plot the correct data for 2D DataArrays (:issue:`3334`). By `Tom Nicholas `_. +- Fix error in concatenating unlabeled dimensions (:pull:`3362`). + By `Deepak Cherian `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/concat.py b/xarray/core/concat.py index 5c9beda3f74..75c72c99a42 100644 --- a/xarray/core/concat.py +++ b/xarray/core/concat.py @@ -177,8 +177,6 @@ def _calc_concat_over(datasets, dim, dim_names, data_vars, coords, compat): if dim not in ds.dims: if dim in ds: ds = ds.set_coords(dim) - else: - raise ValueError("%r is not present in all datasets" % dim) concat_over.update(k for k, v in ds.variables.items() if dim in v.dims) concat_dim_lengths.append(ds.dims.get(dim, 1)) @@ -362,12 +360,21 @@ def ensure_common_dims(vars): # n.b. this loop preserves variable order, needed for groupby. for k in datasets[0].variables: if k in concat_over: - vars = ensure_common_dims([ds.variables[k] for ds in datasets]) + try: + vars = ensure_common_dims([ds.variables[k] for ds in datasets]) + except KeyError: + raise ValueError("%r is not present in all datasets." % k) combined = concat_vars(vars, dim, positions) assert isinstance(combined, Variable) result_vars[k] = combined result = Dataset(result_vars, attrs=result_attrs) + absent_coord_names = coord_names - set(result.variables) + if absent_coord_names: + raise ValueError( + "Variables %r are coordinates in some datasets but not others." + % absent_coord_names + ) result = result.set_coords(coord_names) result.encoding = result_encoding diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index d2635e4451a..1114027387e 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -42,8 +42,10 @@ def test_concat_compat(): for var in ["has_x", "no_x_y"]: assert "y" not in result[var] + with raises_regex(ValueError, "coordinates in some datasets but not others"): + concat([ds1, ds2], dim="q") with raises_regex(ValueError, "'q' is not present in all datasets"): - concat([ds1, ds2], dim="q", data_vars="all", compat="broadcast_equals") + concat([ds2, ds1], dim="q") class TestConcatDataset: @@ -90,7 +92,11 @@ def test_concat_coords_kwarg(self, data, dim, coords): assert_equal(data["extra"], actual["extra"]) def test_concat(self, data): - split_data = [data.isel(dim1=slice(3)), data.isel(dim1=slice(3, None))] + split_data = [ + data.isel(dim1=slice(3)), + data.isel(dim1=3), + data.isel(dim1=slice(4, None)), + ] assert_identical(data, concat(split_data, "dim1")) def test_concat_dim_precedence(self, data):