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

ENH: Preserve monotonic descending index order when merging #2972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ def align(*objects, join='inner', copy=True, indexes=None, exclude=frozenset(),
'indexes along dimension {!r} are not equal'
.format(dim))
index = joiner(matching_indexes)
# Pandas.Index.union gets called for an outer join, and the index
# is sorted in ascending order. When all indexes are monotonic
# descending, it's desirable to keep maintain descending order.
# The ascending sort doesn't happen if only one index is joined.
if join == 'outer' and len(matching_indexes) > 1:
if all((matching_index.is_monotonic_decreasing
for matching_index in matching_indexes)):
index = index[::-1]
joined_indexes[dim] = index
else:
index = matching_indexes[0]
Expand Down
8 changes: 4 additions & 4 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,13 +1851,13 @@ def test_broadcast_misaligned(self):
coords={'y': [1, 2], 'x': [10, -3]})})
x2, y2 = broadcast(x, y)
expected_x2 = Dataset(
{'foo': DataArray([[3, 3], [2, 2], [1, 1], [np.nan, np.nan]],
{'foo': DataArray([[np.nan, np.nan], [1, 1], [2, 2], [3, 3]],
dims=['x', 'y'],
coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})})
coords={'y': [1, 2], 'x': [10, -1, -2, -3]})})
expected_y2 = Dataset(
{'bar': DataArray(
[[2, 4], [np.nan, np.nan], [np.nan, np.nan], [1, 3]],
dims=['x', 'y'], coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})})
[[1, 3], [np.nan, np.nan], [np.nan, np.nan], [2, 4]],
dims=['x', 'y'], coords={'y': [1, 2], 'x': [10, -1, -2, -3]})})
assert_identical(expected_x2, x2)
assert_identical(expected_y2, y2)

Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def test_merge_alignment_error(self):
with raises_regex(ValueError, 'indexes .* not equal'):
xr.merge([ds, other], join='exact')

def test_merge_monotonic_descending_indexes(self):
ds = xr.Dataset(coords={'x': [2, 1]})
other = xr.Dataset(coords={'x': [3, 2]})
actual = xr.merge([ds, other])
expected = xr.Dataset(coords={'x': [3, 2, 1]})
assert expected.identical(actual)

def test_merge_no_conflicts_single_var(self):
ds1 = xr.Dataset({'a': ('x', [1, 2]), 'x': [0, 1]})
ds2 = xr.Dataset({'a': ('x', [2, 3]), 'x': [1, 2]})
Expand Down