Skip to content

da.Array.__setitem__ with bool mask changes dtype and shape #11724

Closed

Description

@crusaderky

When da.Array.__setitem__ receives a dask array of bools as the index, it's internally implemented as da.where.
The logic to retain the object's dtype is missing:

>>> import numpy as np
>>> a = np.asarray([1,2])
>>> a[[True, False]] = 3.3
>>> a
array([3, 2])

>>> import dask.array as da
>>> a = da.asarray([1, 2])
>>> a[da.asarray([True, False])] = 3.3
>>> a
dask.array<where, shape=(2,), dtype=float64, chunksize=(2,), chunktype=numpy.ndarray>
>>> a.compute()
array([3.3, 2. ])

Activity

  1. added
    needs triage
    on Feb 7, 2025
  2. added
    bug
    p2
    and removed
    needs triage
    on Feb 7, 2025
  3. changed the title [-]`da.Array.__setitem__` with bool mask changes dtype[/-] [+]`da.Array.__setitem__` with bool mask changes dtype and shape[/+] on Feb 7, 2025
  4. crusaderky commented on Feb 7, 2025

    @crusaderky
    CollaboratorAuthor

    There is also no protection against shape changes:

    >>> import dask.array as da
    >>> a = da.asarray([1, 2])
    >>> idx = da.asarray([[True], [False]])
    >>> a[idx] = 3  # Should crash here but doesn't
    >>> a
    dask.array<where, shape=(2, 2), dtype=int64, chunksize=(2, 2), chunktype=numpy.ndarray>
  5. crusaderky commented on Feb 7, 2025

    @crusaderky
    CollaboratorAuthor

    This is also wrong:

    >>> a = da.asarray([1, 2, 3])
    >>> idx = da.asarray([True, False, True])
    >>> a[idx] = da.asarray([4, 5, 6])  # Should crash but doesn't
    >>> a
    Out[9]: dask.array<where, shape=(3,), dtype=int64, chunksize=(3,), chunktype=numpy.ndarray>
    >>> a.compute()
    array([4, 2, 6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraybugp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions