[Numpy-discussion] A bug in numpy.random.shuffle?

Charles R Harris charlesr.harris at gmail.com
Thu Sep 5 14:52:22 EDT 2013


On Thu, Sep 5, 2013 at 12:35 PM, Jonathan Helmus <jjhelmus at gmail.com> wrote:

>  On 09/05/2013 01:29 PM, Warren Weckesser wrote:
>
>
>
> On Thu, Sep 5, 2013 at 2:11 PM, Fernando Perez <fperez.net at gmail.com>wrote:
>
>> Hi all,
>>
>> I just ran into this rather weird behavior:
>>
>> http://nbviewer.ipython.org/6453869
>>
>> In summary, as far as I can tell, shuffle is misbehaving when acting
>> on arrays that have structured dtypes. I've seen the problem on 1.7.1
>> (official on ubuntu 13.04) as well as master as of a few minutes ago.
>>
>> Is this my misuse? It really looks like a bug to me...
>>
>>
>
>  Definitely a bug:
>
> In [1]: np.__version__
> Out[1]: '1.9.0.dev-573b3b0'
>
> In [2]: z = np.array([(0,),(1,),(2,),(3,),(4,)], dtype=[('a',int)])
>
> In [3]: z
> Out[3]:
> array([(0,), (1,), (2,), (3,), (4,)],
>       dtype=[('a', '<i8')])
>
> In [4]: shuffle(z)
>
> In [5]: z
> Out[5]:
> array([(0,), (1,), (2,), (0,), (0,)],
>       dtype=[('a', '<i8')])
>
>
>
>  Nothing in the docstring suggests that it shouldn't work for structured
> dtypes.
>
> Warren
>
>
> Looks to stemming from the fact that elements of records arrays cannot be
> swapped:
>
> In [1]: import numpy as np
>
> In [2]: x = np.zeros(5, dtype=[('n', 'S1'), ('i', int)])
>
> In [3]: x['i'] = range(5)
>
> In [4]: print x
>
> [('', 0) ('', 1) ('', 2) ('', 3) ('', 4)]
>
> In [5]: x[0], x[1] = x[1], x[0]
>
> In [6]: print x
>
> [('', 1) ('', 1) ('', 2) ('', 3) ('', 4)]
>
> This is with numpy 1.7.1
>
>
The elements seem to be by reference, making copies works.

In [16]: x[0], x[1] = x[1].copy(), x[0].copy()

In [17]: x
Out[17]:
array([('', 1), ('', 0), ('', 2), ('', 3), ('', 4)],
      dtype=[('n', 'S1'), ('i', '<i8')])

This looks like it might be a bug in creating void scalars.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130905/c1cbbcfc/attachment.html>


More information about the NumPy-Discussion mailing list