Boolean index assignment fails for values of `ndim>1`
- Dominant language
- Python
- Stars
- 13.9k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
This works in NumPy, but fails in Dask.
```python
In [1]: import numpy as np
In [2]: import dask.array as da
In [3]: x = np.asarray([[1, 2]])
In [4]: y = np.asarray([[3, 4]])
In [5]: i = np.asarray([True])
In [6]: xd = da.asarray(x)
In [7]: yd = da.asarray(y)
In [8]: id = da.asarray(i)
In [9]: x[i] = y[i]
In [10]: x
Out[10]: array([[3, 4]])
In [11]: xd[id] = yd[id]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 xd[id] = yd[id]
File ~/programming/pixi-dev-scipystack/scipy/.pixi/envs/array-api/lib/python3.12/site-packages/dask/array/core.py:1903, in Array.__setitem__(self, key, value)
1900 from dask.array.routines import where
1902 if isinstance(value, Array) and value.ndim > 1:
-> 1903 raise ValueError("boolean index array should have 1 dimension")
1904 try:
1905 y = where(key, value, self)
ValueError: boolean index array should have 1 dimension
In [12]: yd[id].ndim
Out[12]: 2
```
**Anything else we need to know?**:
I think the error message is unclear too. I initially thought that the "boolean index array" referred to `id` here (the index array), as opposed to `yd[id]`, the value array being assigned.
Contributor guide
Research direction
Start at dask/array/core.py around the __setitem__ path shown in the traceback, then reproduce the xd[id] = yd[id] example from the issue. Compare the assignment with NumPy's result and clarify the reported error; done means the ndim>1 value assignment succeeds consistently with NumPy and the error wording no longer misidentifies the boolean index array.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100