[Numpy-discussion] A bug in numpy.random.shuffle?
Charles R Harris
charlesr.harris at gmail.com
Thu Sep 5 14:43:12 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
>
>
Oh, nice one ;) Should be fixable if you want to submit a patch.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130905/a2913155/attachment.html>
More information about the NumPy-Discussion
mailing list