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 751732952,MDU6SXNzdWU3NTE3MzI5NTI=,4612,Assigning nan to int-dtype array converts nan to int,500246,open,0,,,1,2020-11-26T17:00:45Z,2021-01-02T03:55:30Z,,CONTRIBUTOR,,,,"(I am almost sure this already exists as an issue, but I can't find the original) **What happened**: When assigning nan to a integer-dtype array, the nan gets incorrectly inverted to int. **What you expected to happen**: I expect to get a `ValueError`, like I get for pure numpy arrays. **Minimal Complete Verifiable Example**: ```python import xarray import numpy da = xarray.DataArray(numpy.array([0,1], dtype=""u8"")) da[0] = numpy.nan print(da) ``` Gives: ``` array([9223372036854775808, 1], dtype=uint64) Dimensions without coordinates: dim_0 ``` **Anything else we need to know?**: In numpy the equivalent code raises `ValueError` This is related but different from #2945. In #2945, xarray behaves the same as numpy. In #4612, xarray behaves differently from numpy. **Environment**:
Output of xr.show_versions() INSTALLED VERSIONS ------------------ commit: None python: 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05) [GCC 7.5.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.6 libnetcdf: 4.7.4 xarray: 0.16.1 pandas: 1.1.4 numpy: 1.19.4 scipy: 1.5.3 netCDF4: 1.5.4 pydap: None h5netcdf: 0.8.1 h5py: 3.1.0 Nio: None zarr: 2.5.0 cftime: 1.2.1 nc_time_axis: None PseudoNetCDF: None rasterio: 1.1.7 cfgrib: None iris: None bottleneck: None dask: 2.30.0 distributed: 2.30.1 matplotlib: 3.3.2 cartopy: 0.18.0 seaborn: None numbagg: None pint: None setuptools: 49.6.0.post20201009 pip: 20.2.4 conda: installed pytest: 6.1.2 IPython: 7.19.0 sphinx: 3.3.0
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/4612/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,issue 376104925,MDU6SXNzdWUzNzYxMDQ5MjU=,2529,numpy.insert on DataArray may silently result in array inconsistent with its coordinates ,500246,closed,0,,,1,2018-10-31T18:33:23Z,2020-11-07T21:55:42Z,2020-11-07T21:55:42Z,CONTRIBUTOR,,,,"```python import numpy import xarray da = xarray.DataArray(numpy.arange(10*3).reshape(10, 3), dims=(""x"", ""y""), coords={""foo"": ((""x"", ""y""), numpy.arange(3*10).reshape(10,3))}) print(da.shape == da[""foo""].shape) da2 = numpy.insert(da, 3, 0, axis=0) print(da2.shape == da2[""foo""].shape) ``` #### Problem description Running the code snippet gives ``` True False ``` and does not raise any exception. In the resulting `da2`, the shape for `da2` and `da2['foo']` are different: we have changed the size of `da2` without changing the size of its corresponding `foo` coordinate. This happens silently, no exception is thrown. Inevitably, this is likely to result in problems at a later stage. #### Expected Output I would expect to get an exception, telling me that the insertion has failed because there are coordinates associated with the axis along which we are inserting values. It would be nice to have an `xarray.insert` that can handle this, for example, by forcing us to provide corresponding insertion values for the coordinates. #### Output of ``xr.show_versions()``
INSTALLED VERSIONS ------------------ commit: None python: 3.7.0.final.0 python-bits: 64 OS: Linux OS-release: 2.6.32-754.el6.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8 xarray: 0.10.7 pandas: 0.23.2 numpy: 1.15.2 scipy: 1.1.0 netCDF4: 1.4.0 h5netcdf: 0.6.1 h5py: 2.8.0 Nio: None zarr: None bottleneck: 1.2.1 cyordereddict: None dask: 0.18.1 distributed: 1.22.0 matplotlib: 3.0.0 cartopy: 0.16.0 seaborn: 0.9.0 setuptools: 39.2.0 pip: 18.0 conda: None pytest: 3.2.2 IPython: 6.4.0 sphinx: None
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/2529/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue 410317757,MDU6SXNzdWU0MTAzMTc3NTc=,2772,Should xarray allow assigning a masked constant?,500246,open,0,,,1,2019-02-14T14:10:20Z,2019-02-15T20:24:44Z,,CONTRIBUTOR,,,,"Currently, `ds['a'] = ((), ma.masked)` where `ds` is an `xarray.Dataset` gives `ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): ((), masked) to Variable.`, whereas `ds['a'] = (), ma.MaskedArray(0.0, True)` works (it sets the indicated value to NaN). Should assigning `ma.masked` be equivalent to assigning `ma.MaskedArray(0.0, True)`, or are there good reasons for the difference in behaviour?","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/2772/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,issue