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

xarray.where() drops attributes #4141

Closed
davidbrochart opened this issue Jun 10, 2020 · 3 comments · Fixed by #4687
Closed

xarray.where() drops attributes #4141

davidbrochart opened this issue Jun 10, 2020 · 3 comments · Fixed by #4687
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@davidbrochart
Copy link
Contributor

MCVE Code Sample

import xarray as xr

da = xr.DataArray(1)
da.attrs['foo'] = 'bar'
xr.where(da==0, -1, da).attrs
# shows: {}

Expected Output

{'foo': 'bar'}

Problem Description

I would expect the attributes to remain in the data array.

Versions

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 08:20:52)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-33-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.15.1
pandas: 1.0.4
numpy: 1.18.4
scipy: 1.4.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.4
cfgrib: None
iris: None
bottleneck: None
dask: 2.16.0
distributed: None
matplotlib: 3.2.1
cartopy: None
seaborn: None
numbagg: None
setuptools: 46.2.0
pip: 20.1
conda: None
pytest: None
IPython: 7.14.0
sphinx: 3.0.4

@keewis
Copy link
Collaborator

keewis commented Jun 11, 2020

you can work around this by using the where method instead of the global xr.where function:

In [8]: da.where(da == 0, -1).attrs
Out[8]: {'foo': 'bar'}

For more information on the current state of attribute propagation, see #3891.

@davidbrochart
Copy link
Contributor Author

Thanks a lot @keewis !

@keewis
Copy link
Collaborator

keewis commented Dec 13, 2020

reopening so this can be closed by #4687

@keewis keewis reopened this Dec 13, 2020
@dcherian dcherian added the topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) label Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants