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

keep_attrs not respected for unary operators #4065

Closed
gerritholl opened this issue May 15, 2020 · 2 comments · Fixed by #4195
Closed

keep_attrs not respected for unary operators #4065

gerritholl opened this issue May 15, 2020 · 2 comments · Fixed by #4195
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@gerritholl
Copy link
Contributor

The xarray global option keep_attrs (introduced in #2482 ) is not respected for unary operators.

MCVE Code Sample

import xarray as xr
x = xr.DataArray([1, 2, 3], attrs={"A": "B"})
with xr.set_options(keep_attrs=True):
    y = ~x
print(x.attrs, y.attrs)

Expected Output

I expect

{'A': 'B'} {'A': 'B'}

Problem Description

I get:

{'A': 'B'} {}

I get the same for the other unary operators +x, -x, and abs(x).

Versions

Tested with latest xarray master (see below for details).

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.2 | packaged by conda-forge | (default, Mar 23 2020, 18:16:37)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 4.12.14-lp150.12.82-default
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.4

xarray: 0.15.2.dev64+g2542a63f
pandas: 1.0.3
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.1.1.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.3
cfgrib: None
iris: None
bottleneck: None
dask: 2.14.0
distributed: 2.14.0
matplotlib: 3.2.1
cartopy: 0.17.0
seaborn: None
numbagg: None
pint: None
setuptools: 46.1.3.post20200325
pip: 20.0.2
conda: installed
pytest: 5.4.1
IPython: 7.13.0
sphinx: None

@TomNicholas TomNicholas added the topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) label May 16, 2020
@TomNicholas
Copy link
Member

Thanks for flagging this.

Question for other devs: Where are these unary operators on xarray objects defined? If I search __neg__ I don't get anything, are they inherited somehow?

@keewis
Copy link
Collaborator

keewis commented May 16, 2020

they're defined mentioned here:

UNARY_OPS = ["neg", "pos", "abs", "invert"]

and injected:

xarray/xarray/core/ops.py

Lines 326 to 328 in 2542a63

# patch in standard special operations
for name in UNARY_OPS:
setattr(cls, op_str(name), cls._unary_op(get_op(name)))

dcherian added a commit to dcherian/xarray that referenced this issue Jul 2, 2020
dcherian added a commit to dcherian/xarray that referenced this issue Aug 15, 2020
dcherian added a commit that referenced this issue Oct 14, 2020
* Propagate attrs with unary, binary functions

Closes #3490
Closes #4065
Closes #3433
Closes #3595

* Un xfail test

* bugfix

* Some progress. Still need keep_attrs in DataArray._unary_op

* Fix dataset attrs

* whats-new

* small fix

* Fix imag, real

* fix variable tests

* fix multiple return variables.

* review comments

* Update doc/whats-new.rst

* Propagate attrs with DataArray unary ops

* More tests

* Small cleanup

* Review comments.

* Fix duplication

Co-authored-by: Maximilian Roos <[email protected]>
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