xr.where overrides coordinate attributes with global attributes
What happened?
xr.where(..., keep_attrs=True) overrides coordinate attributes of the result with global attributes from one of its inputs.
What did you expect to happen?
The coordinate attributes to remain unchanged.
Minimal Complete Verifiable Example
import xarray as xr
x_coord = xr.DataArray(
[1, 2, 3],
dims="x",
attrs={"units": "m"}
)
a = xr.DataArray(
[1, 2, 3],
dims="x",
coords={"x": x_coord},
attrs={"units": "K"}
)
res = xr.where(a > 1, a, 0, keep_attrs=True)
assert res.coords["x"].attrs["units"] == "m" # Fails, overridden with "K"
MVCE confirmation
- [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [X] Complete example — the example is self-contained, including all data and the text of any traceback.
- [X] 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
No response
Anything else we need to know?
Not sure if this is a duplicate of https://github.com/pydata/xarray/issues/2245. If so, feel free to close :)
Environment
INSTALLED VERSIONS
commit: None python: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10) [GCC 10.3.0] python-bits: 64 OS: Linux OS-release: 3.10.0-1160.53.1.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: de_DE.UTF-8 LOCALE: ('de_DE', 'UTF-8') libhdf5: 1.10.6 libnetcdf: 4.7.4
xarray: 2022.6.0 pandas: 1.5.0 numpy: 1.23.3 scipy: 1.9.1 netCDF4: 1.5.6 pydap: None h5netcdf: None h5py: 3.3.0 Nio: None zarr: 2.12.0 cftime: 1.6.2 nc_time_axis: None PseudoNetCDF: None rasterio: 1.2.1 cfgrib: None iris: None bottleneck: None dask: 2022.9.1 distributed: 2022.9.1 matplotlib: 3.6.0 cartopy: None seaborn: None numbagg: None fsspec: 2022.8.2 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 65.3.0 pip: 22.2.2 conda: None pytest: 7.1.3 IPython: None sphinx: None