Skip to content

Error message confusing when assigning to array with array of wrong shape #18401

@jorisvandenbossche

Description

@jorisvandenbossche

In the following example I am using setitem to assign array values to a subset of an existing array. The subset to be assigned has shape (4, 2). Assume that by accident I am setting with an array of (2, 4), then I get an error:

In [6]: arr = np.arange(12).reshape(4, 3)

In [7]: arr[:, [1, 2]] = np.arange(8).reshape(2, 4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-1f9697e767d9> in <module>
----> 1 arr[:, [1, 2]] = np.arange(8).reshape(2, 4)

ValueError: shape mismatch: value array of shape (2,4)  could not be broadcast to indexing result of shape (2,4)

But the error message is a bit confusing: it seems to have swapped to order of the dimensions in the shape? (it says it could not be broadcast to (2, 4), instead of (4,2) ?)


1.20.0 3.8.6 | packaged by conda-forge | (default, Nov 27 2020, 19:31:52)
[GCC 9.3.0]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions