Skip to content

Error when attempting to rename quantified coordinate #350

@simonkeys

Description

@simonkeys

Renaming of coordinates of a quantified DataArray seems to not work:

import xarray as xr
import pint_xarray

da = xr.DataArray([1, 2, 3], coords=[("x", [1,2,3], {"units": "s"})], attrs={"units": 'm'}).pint.quantify()
da.rename(x='y')

raises the error

KeyError                                  Traceback (most recent call last)
Cell In[9], [line 5](vscode-notebook-cell:?execution_count=9&line=5)
      2 import pint_xarray
      4 da = xr.DataArray([1, 2, 3], coords=[("x", [1,2,3], {"units": "s"})], attrs={"units": 'm'}).pint.quantify()
----> [5](vscode-notebook-cell:?execution_count=9&line=5) da.rename(x='y')

File ~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataarray.py:2594, in DataArray.rename(self, new_name_or_name_dict, **names)
   2591 if utils.is_dict_like(new_name_or_name_dict) or new_name_or_name_dict is None:
   2592     # change dims/coords
   2593     name_dict = either_dict_or_kwargs(new_name_or_name_dict, names, "rename")
-> [2594](~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataarray.py:2594)     dataset = self._to_temp_dataset()._rename(name_dict)
   2595     return self._from_temp_dataset(dataset)
   2596 if utils.hashable(new_name_or_name_dict) and names:
   2597     # change name + dims/coords

File ~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4206, in Dataset._rename(self, name_dict, **names)
   4197     if create_dim_coord:
   4198         warnings.warn(
   4199             f"rename {k!r} to {name_dict[k]!r} does not create an index "
   4200             "anymore. Try using swap_dims instead or use set_index "
   (...)   4203             stacklevel=3,
   4204         )
-> [4206](~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4206) variables, coord_names, dims, indexes = self._rename_all(
   4207     name_dict=name_dict, dims_dict=name_dict
   4208 )
   4209 return self._replace(variables, coord_names, dims=dims, indexes=indexes)

File ~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4161, in Dataset._rename_all(self, name_dict, dims_dict)
   4158 variables, coord_names = self._rename_vars(name_dict, dims_dict)
   4159 dims = self._rename_dims(dims_dict)
-> [4161](~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4161) indexes, index_vars = self._rename_indexes(name_dict, dims_dict)
   4162 variables = {k: index_vars.get(k, v) for k, v in variables.items()}
   4164 return variables, coord_names, dims, indexes

File ~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4140, in Dataset._rename_indexes(self, name_dict, dims_dict)
   4138     new_coord_names = [name_dict.get(k, k) for k in coord_names]
   4139     indexes.update(dict.fromkeys(new_coord_names, new_index))
-> [4140](~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/xarray/core/dataset.py:4140)     new_index_vars = new_index.create_variables(
   4141         {
   4142             new: self._variables[old]
   4143             for old, new in zip(coord_names, new_coord_names, strict=True)
   4144         }
   4145     )
   4146     variables.update(new_index_vars)
   4148 return indexes, variables

File ~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/pint_xarray/index.py:36, in PintIndex.create_variables(self, variables)
     34 index_vars_units = {}
     35 for name, var in index_vars.items():
---> [36](~/miniconda3/envs/conda-3.13-srt/lib/python3.13/site-packages/pint_xarray/index.py:36)     data = conversion.array_attach_units(var.data, self.units[name])
     37     var_units = Variable(var.dims, data, attrs=var.attrs, encoding=var.encoding)
     38     index_vars_units[name] = var_units

KeyError: 'y'

This is with xarray 2025.11.0 and pint-xarray 0.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions