Skip to content

Commit 132733a

Browse files
dcherianJoe Hamman
authored andcommitted
Fix concat bug when concatenating unlabeled dimensions. (#3362)
* Fix concat bug when concatenating unlabeled dimensions. * Add whats-new * Add back older test. * fix test * Revert "fix test" This reverts commit c33ca34. * better fix
1 parent 6fb272c commit 132733a

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Bug fixes
6767
- Line plots with the ``x`` or ``y`` argument set to a 1D non-dimensional coord
6868
now plot the correct data for 2D DataArrays
6969
(:issue:`3334`). By `Tom Nicholas <http://github.com/TomNicholas>`_.
70+
- Fix error in concatenating unlabeled dimensions (:pull:`3362`).
71+
By `Deepak Cherian <https://github.com/dcherian/>`_.
7072

7173
Documentation
7274
~~~~~~~~~~~~~

xarray/core/concat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ def _calc_concat_over(datasets, dim, dim_names, data_vars, coords, compat):
177177
if dim not in ds.dims:
178178
if dim in ds:
179179
ds = ds.set_coords(dim)
180-
else:
181-
raise ValueError("%r is not present in all datasets" % dim)
182180
concat_over.update(k for k, v in ds.variables.items() if dim in v.dims)
183181
concat_dim_lengths.append(ds.dims.get(dim, 1))
184182

@@ -362,12 +360,21 @@ def ensure_common_dims(vars):
362360
# n.b. this loop preserves variable order, needed for groupby.
363361
for k in datasets[0].variables:
364362
if k in concat_over:
365-
vars = ensure_common_dims([ds.variables[k] for ds in datasets])
363+
try:
364+
vars = ensure_common_dims([ds.variables[k] for ds in datasets])
365+
except KeyError:
366+
raise ValueError("%r is not present in all datasets." % k)
366367
combined = concat_vars(vars, dim, positions)
367368
assert isinstance(combined, Variable)
368369
result_vars[k] = combined
369370

370371
result = Dataset(result_vars, attrs=result_attrs)
372+
absent_coord_names = coord_names - set(result.variables)
373+
if absent_coord_names:
374+
raise ValueError(
375+
"Variables %r are coordinates in some datasets but not others."
376+
% absent_coord_names
377+
)
371378
result = result.set_coords(coord_names)
372379
result.encoding = result_encoding
373380

xarray/tests/test_concat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ def test_concat_compat():
4242
for var in ["has_x", "no_x_y"]:
4343
assert "y" not in result[var]
4444

45+
with raises_regex(ValueError, "coordinates in some datasets but not others"):
46+
concat([ds1, ds2], dim="q")
4547
with raises_regex(ValueError, "'q' is not present in all datasets"):
46-
concat([ds1, ds2], dim="q", data_vars="all", compat="broadcast_equals")
48+
concat([ds2, ds1], dim="q")
4749

4850

4951
class TestConcatDataset:
@@ -90,7 +92,11 @@ def test_concat_coords_kwarg(self, data, dim, coords):
9092
assert_equal(data["extra"], actual["extra"])
9193

9294
def test_concat(self, data):
93-
split_data = [data.isel(dim1=slice(3)), data.isel(dim1=slice(3, None))]
95+
split_data = [
96+
data.isel(dim1=slice(3)),
97+
data.isel(dim1=3),
98+
data.isel(dim1=slice(4, None)),
99+
]
94100
assert_identical(data, concat(split_data, "dim1"))
95101

96102
def test_concat_dim_precedence(self, data):

0 commit comments

Comments
 (0)