Skip to content

Dask interprets boolean array as an index array #8532

Closed

Description

@abergou

What happened: setting items on a dask array with a bool array incorrectly casts the array to an index array.

What you expected to happen: no broadcastig error.

Minimal Complete Verifiable Example:

>>> import dask
>>> import dask.array as da
>>> arr_10_20 = da.random.random((10, 20))
>>> mask_10 = da.from_array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1]).astype(bool)
>>> val_5_20 = da.random.random((5, 20))
>>> arr_10_20[mask_10, :] = val_5_20
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
...
ValueError: Can't broadcast data with shape (5, 20) across shape (10, 20)

Anything else we need to know?:

Environment:

  • Dask version: 2021.12.0
  • Python version: 3.9
  • Operating System: linux
  • Install method (conda, pip, source): pip

Activity

  1. jsignell commented on Jan 5, 2022

    @jsignell
    Member

    Thanks for writing this up! I am able to reproduce, but I'm having trouble tracking down the exact issue. I think it is likely in this funciton:

    def parse_assignment_indices(indices, shape):
    Maybe @davidhassell will be able to get to the bottom of it :)

    As a workaround, using a computed array as the key for the set item seems to work as expected:

    arr_10_20[mask_10.compute(), :] = val_5_20
  2. pavithraes commented on Jan 5, 2022

    @pavithraes
    Member

    This may be a duplicate of #8460

  3. jsignell commented on Jan 5, 2022

    @jsignell
    Member

    Thanks for mentioning that @pavithraes they are indeed very similar, but follow different code paths. Someone might be able to fix them both at once though!

  4. davidhassell commented on Jan 6, 2022

    @davidhassell
    Contributor

    I'm happy to have to a look at this.

  5. davidhassell commented on Jan 6, 2022

    @davidhassell
    Contributor

    OK - I think I've got to the bottom of this. The problem is related to the parsing of the indices, rather than the mechanics of the assignment (which is a relief!). PR to follow.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions