Skip to content

Commit

Permalink
Hotfix for case of combining identical non-monotonic coords (#3151)
Browse files Browse the repository at this point in the history
* New test for issue

* Fix

* Removed redundant if statement

* Trigger tests rerun

* Updated what's new
  • Loading branch information
TomNicholas authored Jul 31, 2019
1 parent 86799b7 commit e520534
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Bug fixes
- XFAIL several tests which are expected to fail on ARM systems
due to a ``datetime`` issue in NumPy (:issue:`2334`).
By `Graham Inggs <https://github.com/ginggs>`_.
- Fixed bug in ``combine_by_coords()`` causing a `ValueError` if the input had
an unused dimension with coordinates which were not monotonic (:issue`3150`).
By `Tom Nicholas <http://github.com/TomNicholas>`_.

.. _whats-new.0.12.3:

Expand Down
15 changes: 7 additions & 8 deletions xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,13 @@ 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:
if dim in concatenated:
indexes = concatenated.indexes.get(dim)
if not (indexes.is_monotonic_increasing
or indexes.is_monotonic_decreasing):
raise ValueError("Resulting object does not have monotonic"
" global indexes along dimension {}"
.format(dim))
for dim in concat_dims:
indexes = concatenated.indexes.get(dim)
if not (indexes.is_monotonic_increasing
or indexes.is_monotonic_decreasing):
raise ValueError("Resulting object does not have monotonic"
" global indexes along dimension {}"
.format(dim))
concatenated_grouped_by_data_vars.append(concatenated)

return merge(concatenated_grouped_by_data_vars, compat=compat,
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,25 @@ def test_infer_order_from_coords(self):
expected = data
assert expected.broadcast_equals(actual)

def test_combine_leaving_bystander_dimensions(self):
# Check non-monotonic bystander dimension coord doesn't raise
# ValueError on combine (https://github.com/pydata/xarray/issues/3150)
ycoord = ['a', 'c', 'b']

data = np.random.rand(7, 3)

ds1 = Dataset(data_vars=dict(data=(['x', 'y'], data[:3, :])),
coords=dict(x=[1, 2, 3], y=ycoord))

ds2 = Dataset(data_vars=dict(data=(['x', 'y'], data[3:, :])),
coords=dict(x=[4, 5, 6, 7], y=ycoord))

expected = Dataset(data_vars=dict(data=(['x', 'y'], data)),
coords=dict(x=[1, 2, 3, 4, 5, 6, 7], y=ycoord))

actual = combine_by_coords((ds1, ds2))
assert_identical(expected, actual)

def test_combine_by_coords_previously_failed(self):
# In the above scenario, one file is missing, containing the data for
# one year's data for one variable.
Expand Down

0 comments on commit e520534

Please sign in to comment.