DataArray.to_dataset(dim=...) does not preserve attributes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened:
When calling DataArray.to_dataset(dim=...) for splitting the array along one dimension, its attributes are copied to the resulting Dataset but not to the variables.
What you expected to happen:
Attributes are copied to the variables.
Minimal Complete Verifiable Example:
import numpy as np
import xarray as xr
# this setting does not change the behaviour
xr.set_options(keep_attrs=True)
data = np.arange(2*3*4, dtype="uint16").reshape((2,3,4))
coords = [("band", ["1", "2"]), ("y", np.arange(3)), ("x", np.arange(4))]
da = xr.DataArray(data, coords=coords, attrs=dict(myattr=9999))
da

# the attribute is always copied to the Dataset regardless of the promote_attrs value
ds_split = da.to_dataset("band", promote_attrs=False)
ds_split

Anything else we need to know?:
If I avoid the splitting, the attribute is only in the Variable and not in the Dataset.
ds_3d = da.to_dataset(name="ds_3d")
ds_3d

This reproduces the expected behaviour:
ds_sel = xr.Dataset({band: da.sel(band=band, drop=True) for band in da.band.values})
ds_sel

Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:59:51)
[GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 4.15.0-111-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 0.20.1
pandas: 1.3.4
numpy: 1.21.4
scipy: 1.7.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.10
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
setuptools: 59.4.0
pip: 21.3.1
conda: None
pytest: None
IPython: 7.30.0
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the DataArray.to_dataset(dim=...) entry point and run the minimal NumPy/xarray example from the issue, comparing the split Dataset variables with the direct selection result. Trace how attrs are handled when splitting along the dimension and add a regression test that verifies variable attributes are preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100