From ad17931618e4043b424ebe8df2562f232e8fc45d Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Thu, 9 Sep 2021 15:00:22 +0200 Subject: [PATCH] sel: propagate multi-index vars attrs/encoding Related to https://github.com/pydata/xarray/issues/1366 --- xarray/core/dataset.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 6ceef16f122..043bb1650e0 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1178,8 +1178,16 @@ def _overwrite_indexes( new_coord_names = self._coord_names.copy() new_indexes = dict(self.xindexes) + for name, var in variables.items(): + old_var = self._variables.get(name) + if old_var is not None: + # propagate attrs and encoding + # TODO: needs a test + var.attrs = {**old_var.attrs, **var.attrs} + var.encoding = {**old_var.encoding, **var.encoding} + new_variables[name] = var + for name in indexes: - new_variables[name] = variables[name] new_indexes[name] = indexes[name] for name in drop_variables: @@ -2346,6 +2354,8 @@ def isel( indexes.pop(var_name, None) continue if indexes and var_name in indexes: + # TODO benbovy - flexible indexes: this won't be always desirable + # (e.g., 1-d out-of-core coordinate, "meta"-index, etc.) if var_value.ndim == 1: indexes[var_name] = var_value._to_xindex() else: