[NumPy-Tickets] [NumPy] #1366: Sort fails for (big endian) structured arrays

NumPy Trac numpy-tickets@scipy....
Tue Jan 19 08:19:05 CST 2010


#1366: Sort fails for (big endian) structured arrays
------------------------+---------------------------------------------------
 Reporter:  robitaille  |       Owner:  somebody
     Type:  defect      |      Status:  new     
 Priority:  high        |   Milestone:          
Component:  numpy.core  |     Version:  devel   
 Keywords:              |  
------------------------+---------------------------------------------------
 The problem was originally demonstrated on numpy-discussion with the
 following example:

 {{{
 Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
 [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import numpy as np
 >>> data = np.array([('a ', 2.), ('b', 4.), ('d', 3.), ('c', 1.)],
 ...                 dtype=[('col1', '|S5'), ('col2', '>f8')])
 >>>
 >>> data
 array([('a ', 2.0), ('b', 4.0), ('d', 3.0), ('c', 1.0)],
       dtype=[('col1', '|S5'), ('col2', '>f8')])
 >>> data.sort(order=['col1'])
 >>> data
 array([('a ', 2.0), ('b', 4.0), ('c', 1.0), ('d', 3.0)],
       dtype=[('col1', '|S5'), ('col2', '>f8')])
 >>> data.sort(order=['col2'])
 >>> data
 array([('a ', 2.0), ('d', 3.0), ('b', 4.0), ('c', 1.0)],
       dtype=[('col1', '|S5'), ('col2', '>f8')])
 }}}

 In the case of sorting by 'col2', the order is incorrect.

 Another example was demonstrated by Warren Weckesser, which shows that
 this also fails for big-endian integer columns, but that the sort is
 correct if the column is changed to little-endian:

 {{{
 In [137]: z = np.array([(0,),(258,),(256,),(1,),(255,)],
 dtype=[('num','<i2')])

 In [138]: z.sort(order='num')

 In [139]: z
 Out[139]:
 array([(0,), (1,), (255,), (256,), (258,)],
       dtype=[('num', '<i2')])

 In [140]: z = np.array([(0,),(258,),(256,),(1,),(255,)],
 dtype=[('num','>i2')])

 In [141]: z.sort(order='num')

 In [142]: z
 Out[142]:
 array([(255,), (0,), (256,), (1,), (258,)],
       dtype=[('num', '>i2')])

 In [143]: np.__version__
 Out[143]: '1.3.0'
 }}}

 The error was confirmed on two separate numpy installations, one on MacOS
 10.5 with Python 2.5.4, and one on MacOS 10.6 with Python 2.6.1.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1366>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list