[Numpy-discussion] chararray __mod__ behavior

Stéfan van der Walt stefan@sun.ac...
Fri Jul 18 07:32:06 CDT 2008


2008/7/18 Alan McIntyre <alan.mcintyre@gmail.com>:
> This seems odd to me:
>
>>>> A=np.array([['%.3f','%d'],['%s','%r']]).view(np.chararray)
>>>> A % np.array([[1,2],[3,4]])
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/opt/local/lib/python2.5/site-packages/numpy/core/defchararray.py",
> line 126, in __mod__
>    newarr[:] = res
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> Is this expected behavior?  The % gets broadcast as I'd expect for 1D
> arrays, but more dimensions fail as above. Changing the offending line
> in defchararray.py to "newarr.flat = res" makes it behave properly.

That looks like a bug to me.  I would have expected at least one of
the following to work:

A % [[1, 2], [3, 4]]
A % 1
A % (1, 2, 3, 4)

and none of them do.

Stéfan


More information about the Numpy-discussion mailing list