Skip to content

Dataset.pad() does not respect individual data array attributes #7332

Closed
@slavoutich

Description

@slavoutich

What happened?

If we store data in the data array attributes, contrary to the global dataset attributes, it gets wiped after padding.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import xarray as xr

y = xr.DataArray([0.], attrs={"unit": "V"})
x = xr.DataArray([0.], attrs={"unit": "s"})

dataset = xr.Dataset(dict(y=y), coords=dict(x=x), attrs={"foo": "bar"})
assert dataset.foo == "bar"
assert dataset.y.unit == "V"
assert dataset.x.unit == "s"

dataset2 = dataset.pad({"dim_0": (0, 1)})
assert dataset2.foo == "bar"
assert dataset2.y.unit == "V"  # Fails
assert dataset2.x.unit == "s"  # Fails

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.

Relevant log output

AttributeError                            Traceback (most recent call last)
Cell In [1], line 13
     11 dataset2 = dataset.pad({"dim_0": (0, 1)})
     12 assert dataset2.foo == "bar"
---> 13 assert dataset2.y.unit == "V"  # Fails
     14 assert dataset2.x.unit == "s"  # Fails

File ~/.local/opt/conda/envs/test_xarray/lib/python3.10/site-packages/xarray/core/common.py:272, in AttrAccessMixin.__getattr__(self, name)
    270         with suppress(KeyError):
    271             return source[name]
--> 272 raise AttributeError(
    273     f"{type(self).__name__!r} object has no attribute {name!r}"
    274 )

AttributeError: 'DataArray' object has no attribute 'unit'

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC 10.4.0]
python-bits: 64
OS: Linux
OS-release: 6.0.9-zen1-1-zen
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2022.11.0
pandas: 1.5.2
numpy: 1.23.5
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
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
flox: None
numpy_groupies: None
setuptools: 65.5.1
pip: 22.3.1
conda: None
pytest: None
IPython: 8.6.0
sphinx: None

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