[Numpy-discussion] boolean indexing change

Ralf Gommers ralf.gommers at googlemail.com
Sun Jun 10 11:13:28 EDT 2012

Something that we just ran into trying to merge a scipy PR:

With 1.5.1:
>>> np.arange(10)[np.array([0,1,0,1,2,3]) > 0]
array([1, 3, 4, 5])

With current master:
In [1]: np.arange(10)[np.array([0,1,0,1,2,3]) > 0]
ValueError                                Traceback (most recent call last)
/Users/rgommers/<ipython-input-1-49ea14823e3d> in <module>()
----> 1 np.arange(10)[np.array([0,1,0,1,2,3]) > 0]

ValueError: operands could not be broadcast together with shapes (10) (6)

The reason for this is noted in the 2.0.0 release notes:
"Full-array boolean indexing used to allow boolean arrays with a size
non-broadcastable to the array size. Now it forces this to be broadcastable.
Since this affects some legacy code, this change will require discussion
during alpha or early beta testing, and a decision to either keep the
stricter behavior, or add in a hack to allow the previous behavior to

I'm not opposed to the change in principle, but just wanted to note it can
lead to code breaking and puzzled users.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120610/65d7091e/attachment.html>

More information about the NumPy-Discussion mailing list