Skip to content

ERR: Broadcast error on assignment prints dimensions in wrong order #5710

@ischwabacher

Description

@ischwabacher

xref #2357

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.9.1'

In [3]: arr = np.zeros([5, 2])

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

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

In [5]: arr[:,[0,1]] + np.arange(10).reshape([2, 5])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-8c656689a6ee> in <module>()
----> 1 arr[:,[0,1]] + np.arange(10).reshape([2, 5])

ValueError: operands could not be broadcast together with shapes (5,2) (2,5)  # correct

The first error message is confusing at best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions