Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
slavoutich opened this issue Nov 29, 2022 · 1 comment · Fixed by #7267
Closed
4 tasks done

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

slavoutich opened this issue Nov 29, 2022 · 1 comment · Fixed by #7267
Labels

Comments

@slavoutich
Copy link

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

@slavoutich slavoutich added bug needs triage Issue that has not been reviewed by xarray team member labels Nov 29, 2022
@keewis keewis removed the needs triage Issue that has not been reviewed by xarray team member label Nov 29, 2022
@keewis
Copy link
Collaborator

keewis commented Nov 29, 2022

see #7267 for a fix

@keewis keewis linked a pull request Dec 12, 2022 that will close this issue
2 tasks
@keewis keewis closed this as completed Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants