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

Hotfix for case of combining identical non-monotonic coords #3151

Merged
merged 5 commits into from
Jul 31, 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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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