-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
What happened?
A rather obscure issue this time:
When trying to rename a few dimensions and variable names of an old dataset, the resulting xarray lost all attributes on other variables in the dataset.
More specifically, I could narrow it down to a very specific sequence of steps:
- We need a dataset with a multi-index
- We apply swap_dims() to an arbitrary dimension d that is not part of any multi-index
- We rename a variable that is a member of a multi-index that does not have the dimension d neither on any of its levels nor on the overall index.
- Levels within the same multi-index as the last renamed variable lose all of their .attrs.
As far as I could figure out, this does not happen if d is a dimension of any of the levels of the multi-index or the renamed variable has the dimension d.
If the dimension is not first swapped before the variable is renamed, the attributes are preserved.
If there is no multi-index, the issue does not seem to occur.
The attributes are lost upon the call to .rename but also on an equivalent call to .rename_vars.
The loss of attributes seems to only occur for the combination of: Multi-index, swap_dim of unrelated dimension, rename variable in multi-index.
What did you expect to happen?
I expect the attributes of the multi-index levels to persist through the renaming operations.
Specifically, I expect the attributes of variables that are not involved in the renaming operations to not be modified by the renaming since there is no naming conflict.
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import xarray as xr
xr.show_versions()
# your reproducer code ...
ds = xr.Dataset(coords={'time': ('midx_dim', [0.0], {'units': 'fs'}), 'trajid': ('midx_dim', [2], {'long_name': 'Trajectory ID'})})
ds = ds.set_xindex(["time", "trajid"])
print(f"{ds.time.attrs=}") # Output: ds.time.attrs={'units': 'fs'}
ds = ds.assign(hello=("nothing", [1,2,3],{"hi":3}))
print(f"{ds.time.attrs=}") # Output: ds.time.attrs={'units': 'fs'}
ds = ds.swap_dims(nothing='hello')
print(f"{ds.time.attrs=}") # Output: ds.time.attrs={'units': 'fs'}
ds = ds.rename(trajid="atrajectory")
print(f"{ds.time.attrs=}") # Output: ds.time.attrs={}Steps to reproduce
See the minimum example above:
We create a dataset, build a multi-index, add another variable with unrelated dimension, swap that unrelated dimension, then rename a coordinate in the multi-index and suddenly the attributes of .time and .atrajectory are gone.
In the example I printed only the attributes of .time, but the issue also happens to .trajid/.atrajectory and in another case seems to have affected all levels of multi-indices within the dataset.
There never seemed to be an impact on non-coordinate variables.
Only coordinates were affected
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Anything else we need to know?
To me this implies that it may be an issue of the treatment of multi-indexes either during swap_dim or during the renaming process.
I would assume that create_new_variables for the multi-index may be the culprit and may produce new variables without attributes if indexes have been left in an inconsistent state after swap_dim, but I am not sure.
Environment
Details
INSTALLED VERSIONS ------------------ commit: None python: 3.13.9 (tags/v3.13.9:8183fa5, Oct 14 2025, 14:09:13) [MSC v.1944 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 11 machine: AMD64 processor: AMD64 Family 25 Model 117 Stepping 2, AuthenticAMD byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('de_DE', 'cp1252') libhdf5: None libnetcdf: Nonexarray: 2025.12.0
pandas: 3.0.0
numpy: 2.3.0
scipy: 1.15.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.10.3
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 25.2
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None