Skip to content

Commit e520534

Browse files
authored
Hotfix for case of combining identical non-monotonic coords (#3151)
* New test for issue * Fix * Removed redundant if statement * Trigger tests rerun * Updated what's new
1 parent 86799b7 commit e520534

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Bug fixes
4545
- XFAIL several tests which are expected to fail on ARM systems
4646
due to a ``datetime`` issue in NumPy (:issue:`2334`).
4747
By `Graham Inggs <https://github.com/ginggs>`_.
48+
- Fixed bug in ``combine_by_coords()`` causing a `ValueError` if the input had
49+
an unused dimension with coordinates which were not monotonic (:issue`3150`).
50+
By `Tom Nicholas <http://github.com/TomNicholas>`_.
4851

4952
.. _whats-new.0.12.3:
5053

xarray/core/combine.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,13 @@ def combine_by_coords(datasets, compat='no_conflicts', data_vars='all',
501501
fill_value=fill_value)
502502

503503
# Check the overall coordinates are monotonically increasing
504-
for dim in concatenated.dims:
505-
if dim in concatenated:
506-
indexes = concatenated.indexes.get(dim)
507-
if not (indexes.is_monotonic_increasing
508-
or indexes.is_monotonic_decreasing):
509-
raise ValueError("Resulting object does not have monotonic"
510-
" global indexes along dimension {}"
511-
.format(dim))
504+
for dim in concat_dims:
505+
indexes = concatenated.indexes.get(dim)
506+
if not (indexes.is_monotonic_increasing
507+
or indexes.is_monotonic_decreasing):
508+
raise ValueError("Resulting object does not have monotonic"
509+
" global indexes along dimension {}"
510+
.format(dim))
512511
concatenated_grouped_by_data_vars.append(concatenated)
513512

514513
return merge(concatenated_grouped_by_data_vars, compat=compat,

xarray/tests/test_combine.py

+19
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,25 @@ def test_infer_order_from_coords(self):
581581
expected = data
582582
assert expected.broadcast_equals(actual)
583583

584+
def test_combine_leaving_bystander_dimensions(self):
585+
# Check non-monotonic bystander dimension coord doesn't raise
586+
# ValueError on combine (https://github.com/pydata/xarray/issues/3150)
587+
ycoord = ['a', 'c', 'b']
588+
589+
data = np.random.rand(7, 3)
590+
591+
ds1 = Dataset(data_vars=dict(data=(['x', 'y'], data[:3, :])),
592+
coords=dict(x=[1, 2, 3], y=ycoord))
593+
594+
ds2 = Dataset(data_vars=dict(data=(['x', 'y'], data[3:, :])),
595+
coords=dict(x=[4, 5, 6, 7], y=ycoord))
596+
597+
expected = Dataset(data_vars=dict(data=(['x', 'y'], data)),
598+
coords=dict(x=[1, 2, 3, 4, 5, 6, 7], y=ycoord))
599+
600+
actual = combine_by_coords((ds1, ds2))
601+
assert_identical(expected, actual)
602+
584603
def test_combine_by_coords_previously_failed(self):
585604
# In the above scenario, one file is missing, containing the data for
586605
# one year's data for one variable.

0 commit comments

Comments
 (0)