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

Re-index: fix missing variable metadata #6389

Merged
merged 1 commit into from
Mar 21, 2022
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
7 changes: 7 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,13 @@ def _reindex_callback(
new_variables = variables.copy()
new_indexes = indexes.copy()

# re-assign variable metadata
for name, new_var in new_variables.items():
var = self._variables.get(name)
if var is not None:
new_var.attrs = var.attrs
new_var.encoding = var.encoding

# pass through indexes from excluded dimensions
# no extra check needed for multi-coordinate indexes, potential conflicts
# should already have been detected when aligning the indexes
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,19 @@ def test_reindex(self):
actual = ds.reindex(x=[0, 1, 3], y=[0, 1])
assert_identical(expected, actual)

def test_reindex_attrs_encoding(self):
ds = Dataset(
{"data": ("x", [1, 2, 3])},
{"x": ("x", [0, 1, 2], {"foo": "bar"}, {"bar": "baz"})},
)
actual = ds.reindex(x=[0, 1])
expected = Dataset(
{"data": ("x", [1, 2])},
{"x": ("x", [0, 1], {"foo": "bar"}, {"bar": "baz"})},
)
assert_identical(actual, expected)
assert actual.x.encoding == expected.x.encoding

def test_reindex_warning(self):
data = create_test_data()

Expand Down