Create a boolean DataArray to use as a mask for another DataArray, with the same coordinates:
da_1 = xr.DataArray(np.array([0.861185, 0.301491, 0.642744, 0.773298, 0.786516, 0.011693, 0.659326, 0.07877 , 0.108488, 0.747863]), coords=[('coord1', np.arange(10))], name='da1')
da_2 = xr.DataArray(np.array([0.116959, 0.955742, 0.121562, 0.635573, 0.304895, 0.064529, 0.042461, 0.41751 , 0.607457, 0.110104]), coords=[('coord1', np.arange(10))], name='da2')
da_3 = np.rint(da_2).astype('bool')
print(da_1)
<xarray.DataArray 'da1' (coord1: 10)>
array([0.861185, 0.301491, 0.642744, 0.773298, 0.786516, 0.011693, 0.659326,
0.07877 , 0.108488, 0.747863])
Coordinates:
* coord1 (coord1) int64 0 1 2 3 4 5 6 7 8 9
print(da_1.where(da_3))
<xarray.DataArray (coord1: 10)>
array([ nan, 0.301491, nan, 0.773298, nan, nan, nan,
nan, 0.108488, nan])
Coordinates:
* coord1 (coord1) int64 0 1 2 3 4 5 6 7 8 9
When using a DataArray (da_3 in the example) as the condition in the where() method of another DataArray (da_1), the name of the DataArray being masked (da_1) is dropped.
The name should be retained.
print(da_1.where(da_3))
<xarray.DataArray 'da_1` (coord1: 10)>
array([ nan, 0.301491, nan, 0.773298, nan, nan, nan,
nan, 0.108488, nan])
Coordinates:
* coord1 (coord1) int64 0 1 2 3 4 5 6 7 8 9
xr.show_versions()commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-38-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
xarray: 0.10.3
pandas: 0.22.0
numpy: 1.14.3
scipy: 1.1.0
netCDF4: 1.3.2
h5netcdf: None
h5py: 2.7.1
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.17.4
distributed: 1.21.8
matplotlib: 2.2.2
cartopy: None
seaborn: 0.8.1
setuptools: 39.1.0
pip: 10.0.1
conda: 4.5.2
pytest: 3.5.1
IPython: 6.4.0
sphinx: 1.7.4
Indeed, we should have some sort of logic to indicate that the name from the original DataArray should be preserved.
Under the hood, DataArray.where uses xarray.apply_ufunc. So maybe this should be coupled with keep_attrs=True in xarray.apply_ufunc()?
Yes, I think it makes sense to couple it with keep_attrs=True. One could question whether the name should be considered separate to the attributes (and therefore have an additional keyword argument for keeping the name), but I think in most cases it makes sense to keep the name if you're already keeping the attributes, and to drop the name if you're dropping the attributes.
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity
If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically
This bug appears to have been fixed, as of Xarray 0.15.0.