Skip to content

Commit

Permalink
reindex: fix missing variable metadata (#6389)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy authored Mar 21, 2022
1 parent 511d36c commit c604ee1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,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 @@ -1887,6 +1887,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

0 comments on commit c604ee1

Please sign in to comment.