Skip to content

Commit

Permalink
Fix concat bug when concatenating unlabeled dimensions. (#3362)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dcherian authored and Joe Hamman committed Oct 8, 2019
1 parent 6fb272c commit 132733a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://github.com/TomNicholas>`_.
- Fix error in concatenating unlabeled dimensions (:pull:`3362`).
By `Deepak Cherian <https://github.com/dcherian/>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
13 changes: 10 additions & 3 deletions xarray/core/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions xarray/tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 132733a

Please sign in to comment.