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
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Deprecations

Bug Fixes
~~~~~~~~~
- :py:func:`align` with ``join="override"`` now keeps the attributes and encoding
of each object's own index coordinates, instead of copying (for
:py:class:`DataArray`) or merging in (for :py:class:`Dataset`) those of the
first object (:issue:`6860`, :pull:`11990`).
By `Pierce Luu <https://github.com/pierceluu10>`_.


Documentation
Expand Down
7 changes: 6 additions & 1 deletion xarray/structure/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,18 @@ def override_indexes(self) -> None:
new_indexes = {}
new_variables = {}
matching_indexes = self.objects_matching_indexes[i + 1]
matching_index_vars = self.objects_matching_index_vars[i + 1]

for key, aligned_idx in self.aligned_indexes.items():
obj_idx = matching_indexes.get(key)
if obj_idx is not None:
for name, var in self.aligned_index_vars[key].items():
new_indexes[name] = aligned_idx
new_variables[name] = var.copy(deep=self.copy)
# only override the index, keep the object's own metadata
obj_var = matching_index_vars[key][name]
new_variables[name] = var._replace(
attrs=obj_var.attrs, encoding=obj_var.encoding
).copy(deep=self.copy)

objects[i + 1] = obj._overwrite_indexes(new_indexes, new_variables)

Expand Down
21 changes: 19 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2849,18 +2849,35 @@ def test_align_str_dtype(self) -> None:
assert expected_b.x.dtype == actual_b.x.dtype

@pytest.mark.parametrize("join", ["left", "override"])
def test_align_index_var_attrs(self, join) -> None:
@pytest.mark.parametrize("as_dataarray", [False, True])
def test_align_index_var_attrs(self, join, as_dataarray: bool) -> None:
# regression test https://github.com/pydata/xarray/issues/6852
# aligning two objects should have no side effect on their index variable
# metadata.

ds = Dataset(coords={"x": ("x", [1, 2, 3], {"units": "m"})})
ds.x.encoding = {"dtype": "int32"}
ds_noattr = Dataset(coords={"x": ("x", [1, 2, 3])})

xr.align(ds_noattr, ds, join=join)
objs: tuple[Dataset, Dataset] | tuple[DataArray, DataArray] = (ds_noattr, ds)
if as_dataarray:
objs = (
DataArray([0, 0, 0], coords=ds_noattr.coords),
DataArray([0, 0, 0], coords=ds.coords),
)

# regression test https://github.com/pydata/xarray/issues/6860
# each aligned object keeps the metadata of its own index variable
for first, second in (objs, objs[::-1]):
actual_first, actual_second = xr.align(first, second, join=join)
for actual, expected in [(actual_first, first), (actual_second, second)]:
assert actual.x.attrs == expected.x.attrs
assert actual.x.encoding == expected.x.encoding

assert ds.x.attrs == {"units": "m"}
assert ds.x.encoding == {"dtype": "int32"}
assert ds_noattr.x.attrs == {}
assert ds_noattr.x.encoding == {}

def test_align_scalar_index(self) -> None:
# ensure that indexes associated with scalar coordinates are not ignored
Expand Down
Loading