Skip to content

Flexible indexes: handle renaming coordinate variables #5645

Closed
@benbovy

Description

@benbovy

We should have some API in xarray.Index to update the index when its corresponding coordinate variables are renamed.

This currently implemented here where the underlying pd.Index name(s) are updated:

xarray/xarray/core/dataset.py

Lines 3299 to 3314 in c5530d5

def _rename_indexes(self, name_dict, dims_set):
if self._indexes is None:
return None
indexes = {}
for k, v in self.xindexes.items():
# TODO: benbovy - flexible indexes: make it compatible with any xarray Index
index = v.to_pandas_index()
new_name = name_dict.get(k, k)
if new_name not in dims_set:
continue
if isinstance(index, pd.MultiIndex):
new_names = [name_dict.get(k, k) for k in index.names]
indexes[new_name] = PandasMultiIndex(index.rename(names=new_names))
else:
indexes[new_name] = PandasIndex(index.rename(new_name))
return indexes

This logic should be moved into PandasIndex and PandasMultiIndex.

Other, custom indexes might also have internal attributes to update, so we might need formal API for that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions