id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 1608352581,I_kwDOAMm_X85f3YNF,7581,xr.where loses attributes despite keep_attrs=True,500246,closed,0,,,1,2023-03-03T10:14:34Z,2023-04-06T01:58:45Z,2023-04-06T01:58:45Z,CONTRIBUTOR,,,,"### What happened? I'm using `xarray.where` to mask data: `xr.where(ds == ds.attrs[""_FillValue""], nan, ds)`. This loses the attributes on `ds` even if I pass `keep_attrs=True`. ### What did you expect to happen? I expect that if I use `keep_attrs=True`, either via `xr.set_options` or directly passed to `xr.where`, that attributes on the dataset are retained. ### Minimal Complete Verifiable Example ```Python import xarray as xr a = xr.DataArray([0], attrs={""a"": ""b""}) with xr.set_options(keep_attrs=True): a2 = xr.where(a==0, 0, a, keep_attrs=True) print(a2.attrs) ``` ### 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](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result. - [X] New issue — a search of GitHub Issues suggests this is not a duplicate. ### Relevant log output ```Python {} ``` ### Anything else we need to know? I can make a workaround by turning the logic around, such as `xr.where(a!=0, a, 0)`, which does retain attributes. The workaround works in this case, but `a!=0` is not always the same as `a==0`, so it would be preferable if the attributes were retained either way. ### Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0] python-bits: 64 OS: Linux OS-release: 5.3.18-150300.59.76-default machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: ('en_GB', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.9.1 xarray: 2023.2.0 pandas: 1.5.3 numpy: 1.24.2 scipy: 1.10.1 netCDF4: 1.6.2 pydap: None h5netcdf: 1.1.0 h5py: 3.8.0 Nio: None zarr: 2.13.6 cftime: 1.6.2 nc_time_axis: None PseudoNetCDF: None rasterio: 1.3.6 cfgrib: None iris: None bottleneck: 1.3.6 dask: 2023.2.1 distributed: 2023.2.1 matplotlib: 3.7.0 cartopy: 0.21.1 seaborn: None numbagg: None fsspec: 2023.1.0 cupy: None pint: 0.20.1 sparse: None flox: None numpy_groupies: None setuptools: 67.4.0 pip: 23.0.1 conda: None pytest: 7.2.1 mypy: None IPython: 8.7.0 sphinx: 5.3.0
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/7581/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue