Skip to content

Commit 9dc340e

Browse files
committed
Fix tests.
1 parent 4cb7d02 commit 9dc340e

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

xarray/core/concat.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,17 @@ def determine_dims(datasets, result_coord_names):
318318
both_data_and_coords = result_coord_names & noncoord_names
319319
if both_data_and_coords:
320320
raise ValueError(
321-
"%r is a coordinate in some datasets and not in others."
322-
% list(both_data_and_coords)[0] # preserve previous behaviour
321+
"%r is a coordinate in some datasets but not others."
322+
% list(both_data_and_coords)[0] # preserve format of error message
323323
)
324324
dim_names, result_coords = determine_dims(datasets, result_coord_names)
325325
# we don't want the concat dimension in the result dataset yet
326326
result_coords.pop(dim, None)
327327

328+
# case where concat dimension is a coordinate but not a dimension
329+
if dim in result_coord_names and dim not in dim_names:
330+
datasets = [ds.expand_dims(dim) for ds in datasets]
331+
328332
# determine which variables to concatentate
329333
concat_over, equals = _calc_concat_over(
330334
datasets, dim, dim_names, data_vars, coords, compat
@@ -335,6 +339,10 @@ def determine_dims(datasets, result_coord_names):
335339
if variables_to_merge:
336340
to_merge = []
337341
for ds in datasets:
342+
if variables_to_merge - set(ds.variables):
343+
raise ValueError(
344+
"Encountered unexpected variables %r" % list(variables_to_merge)[0]
345+
)
338346
to_merge.append(ds.reset_coords()[list(variables_to_merge)])
339347
# TODO: Provide equals as an argument and thread that down to merge.unique_variable
340348
result_vars = merge_variables(
@@ -397,6 +405,11 @@ def ensure_common_dims(vars):
397405
result = result.set_coords(result_coord_names)
398406
result.encoding = result_encoding
399407

408+
# TODO: avoid this
409+
unlabeled_dims = dim_names - result_coord_names
410+
result = result.drop(unlabeled_dims, errors="ignore")
411+
412+
# need to pass test when provided dim is a DataArray
400413
if coord is not None:
401414
# add concat dimension last to ensure that its in the final Dataset
402415
result[coord.name] = coord

xarray/tests/test_concat.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import pytest
66

77
from xarray import DataArray, Dataset, Variable, concat
8-
from xarray.core import dtypes
9-
8+
from xarray.core import dtypes, merge
109
from . import (
1110
InaccessibleArray,
1211
assert_array_equal,
@@ -36,11 +35,15 @@ def test_concat_compat():
3635
coords={"x": [0, 1], "y": [1], "z": [-1, -2], "q": [0]},
3736
)
3837

39-
result = concat([ds1, ds2], dim="y", compat="equals")
38+
result = concat([ds1, ds2], dim="y", data_vars="minimal", compat="broadcast_equals")
39+
assert_equal(ds2.no_x_y, result.no_x_y.transpose())
4040

41-
for var in ["has_x", "no_x_y", "const1"]:
41+
for var in ["has_x", "no_x_y"]:
4242
assert "y" not in result[var]
4343

44+
with raises_regex(ValueError, "'q' is not a dimension in all datasets"):
45+
concat([ds1, ds2], dim="q", data_vars="all", compat="broadcast_equals")
46+
4447

4548
class TestConcatDataset:
4649
@pytest.fixture
@@ -116,7 +119,7 @@ def test_concat_coords(self):
116119
actual = concat(objs, dim="x", coords=coords)
117120
assert_identical(expected, actual)
118121
for coords in ["minimal", []]:
119-
with raises_regex(ValueError, "not equal across"):
122+
with raises_regex(merge.MergeError, "conflicting values"):
120123
concat(objs, dim="x", coords=coords)
121124

122125
def test_concat_constant_index(self):
@@ -127,8 +130,10 @@ def test_concat_constant_index(self):
127130
for mode in ["different", "all", ["foo"]]:
128131
actual = concat([ds1, ds2], "y", data_vars=mode)
129132
assert_identical(expected, actual)
130-
with raises_regex(ValueError, "not equal across datasets"):
131-
concat([ds1, ds2], "y", data_vars="minimal")
133+
with raises_regex(merge.MergeError, "conflicting values"):
134+
# previously dim="y", and raised error which makes no sense.
135+
# "foo" has dimension "y" so minimal should concatenate it?
136+
concat([ds1, ds2], "new_dim", data_vars="minimal")
132137

133138
def test_concat_size0(self):
134139
data = create_test_data()
@@ -372,10 +377,10 @@ def setUp(self):
372377

373378
def test_concat_sensible_compat_errors(self):
374379

375-
with raises_regex(ValueError, "'only_x' is not equal across datasets."):
380+
with raises_regex(merge.MergeError, "conflicting values"):
376381
concat(self.dsets, data_vars="sensible", dim="y")
377382

378-
with raises_regex(ValueError, "'coord1' is not equal across datasets."):
383+
with raises_regex(merge.MergeError, "conflicting values"):
379384
concat(self.dsets, coords="sensible", dim="y")
380385

381386
@pytest.mark.parametrize("concat_dim", ["y", "new_dim"])
@@ -431,7 +436,9 @@ def test_compat_override(self, data_vars, coords):
431436

432437
def test_compat_override_different_error(self):
433438
with raises_regex(ValueError, "Cannot specify both .*='different'"):
434-
concat(self.dsets, data_vars="different", compat="override")
439+
concat(
440+
self.dsets, dim="concat_dim", data_vars="different", compat="override"
441+
)
435442

436443

437444
class TestConcatDataArray:

0 commit comments

Comments
 (0)