Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concat bug when concatenating unlabeled dimensions. #3362

Merged
merged 7 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -41,8 +41,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"):
dcherian marked this conversation as resolved.
Show resolved Hide resolved
concat([ds1, ds2], dim="q", data_vars="all", compat="broadcast_equals")
concat([ds2, ds1], dim="q")


class TestConcatDataset:
Expand Down Expand Up @@ -89,7 +91,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