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 1027024891,I_kwDOAMm_X849Nyf7,5866,Element-wise numpy operations using kwarg `where` are not supported for chunked DataArrays,38085953,closed,0,,,3,2021-10-15T02:53:22Z,2022-03-21T09:02:43Z,2022-03-21T09:02:43Z,NONE,,,," **What happened**: In general, element-wise operations work smoothly with DataArrays. However, I have found that using element-wise operations such as `np.divide`, `np.multiply`, etc, with the keyword argument `where` throws an error if the DataArray values are contained in a chunked dask array, as opposed to a numpy array. **What you expected to happen**: I expected the operation `np.divide(da1, da2, where= f(da1))` be the same irrespective of the chunks used to store the data. Below I include a comparison between the behaviors. The essence of the problem is that this operation only works if the number of chunks is one and the data is stored in a numpy array. **Minimal Complete Verifiable Example**: ```python import numpy as np import xarray as xr toy_da = xr.DataArray( np.random.rand(50, 50), coords=([np.arange(50), np.arange(50)]), dims=['x', 'y'], name='var1') toy_da2 = toy_da.chunk({'x': 5}) # This works as expected np.divide(toy_da, toy_da, where=toy_da > 0.5) # This, however, does not np.divide(toy_da2, toy_da2, where=toy_da2 > 0.5) ``` The error message: ```File ""xarray_issue.py"", line 15, in Traceback (most recent call last): File ""xarray_issue.py"", line 15, in np.divide(toy_da2, toy_da2, where=toy_da2 > 0.5) File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/xarray/core/arithmetic.py"", line 89, in __array_ufunc__ keep_attrs=_get_keep_attrs(default=True), File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/xarray/core/computation.py"", line 1180, in apply_ufunc keep_attrs=keep_attrs, File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/xarray/core/computation.py"", line 293, in apply_dataarray_vfunc result_var = func(*data_vars) File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/xarray/core/computation.py"", line 742, in apply_variable_ufunc result_data = func(*input_data) File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/dask/array/core.py"", line 1386, in __array_ufunc__ return elemwise(numpy_ufunc, *inputs, **kwargs) File ""/opt/anaconda3/envs/heatnet/lib/python3.7/site-packages/dask/array/core.py"", line 4282, in elemwise msg % (op.__name__, str(sorted(set(kwargs) - set([""name"", ""dtype""])))) TypeError: true_divide does not take the following keyword arguments ['where'] ``` **Anything else we need to know?**: **Environment**:
Output of xr.show_versions() >>> xr.show_versions() INSTALLED VERSIONS ------------------ commit: None python: 3.7.11 (default, Jul 27 2021, 07:03:16) [Clang 10.0.0 ] python-bits: 64 OS: Darwin OS-release: 20.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.10.6 libnetcdf: 4.8.0 xarray: 0.19.0 pandas: 1.3.2 numpy: 1.18.5 scipy: 1.4.1 netCDF4: 1.5.7 pydap: None h5netcdf: 0.11.0 h5py: 2.10.0 Nio: None zarr: None cftime: 1.5.0 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: 0.9.9.0 iris: None bottleneck: 1.3.2 dask: 2021.08.1 distributed: 2021.08.1 matplotlib: 3.2.0 cartopy: None seaborn: 0.11.2 numbagg: None pint: None setuptools: 52.0.0.post20210125 pip: 21.2.2 conda: None pytest: None IPython: 7.26.0 sphinx: None
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/5866/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue