Skip to content
Open
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
3 changes: 2 additions & 1 deletion xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ def combine_by_coords(datasets, compat='no_conflicts', data_vars='all',
fill_value=fill_value)

# Check the overall coordinates are monotonically increasing
for dim in concatenated.dims:
# Only check dimensions that were used for concatenation
for dim in concat_dims:
if dim in concatenated:
indexes = concatenated.indexes.get(dim)
if not (indexes.is_monotonic_increasing
Expand Down
24 changes: 24 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,30 @@ def test_check_for_impossible_ordering(self):
" along dimension x"):
combine_by_coords([ds1, ds0])

def test_combine_by_coords_non_monotonic_non_concat_dim(self):
# Test that non-monotonic coordinates are allowed on dimensions
# that don't vary between datasets (i.e., not concatenation dims)
# Regression test for GH issue
yCoord = ['a', 'c', 'b'] # non-monotonic

ds1 = Dataset(
data_vars={'data': (['x', 'y'], np.random.rand(3, 3))},
coords={'x': [1, 2, 3], 'y': yCoord}
)

ds2 = Dataset(
data_vars={'data': (['x', 'y'], np.random.rand(4, 3))},
coords={'x': [4, 5, 6, 7], 'y': yCoord}
)

# Should not raise an error - y is not a concatenation dimension
actual = combine_by_coords([ds1, ds2])

# Verify the result has the expected shape and coordinates
assert actual.dims == {'x': 7, 'y': 3}
assert list(actual.coords['x'].values) == [1, 2, 3, 4, 5, 6, 7]
assert list(actual.coords['y'].values) == yCoord


@pytest.mark.filterwarnings("ignore:In xarray version 0.13 `auto_combine` "
"will be deprecated")
Expand Down