[Numpy-discussion] numpy.sort() failing mysteriously

Manolo Martínez manolo at austrohungaro.com
Thu Jun 19 09:20:02 EDT 2014


Dear all,

I have a couple of structured arrays that, as far as I can see, only
differ in irrelevant ways; yet numpy.sort chokes on one, not the other:

This structured array *cannot* be sorted with np.sort(array, order='weight')...

array([([0, 0, 0], 0.0), ([0, 0, 1], 0.0), ([0, 0, 2], 0.0),
       ([0, 1, 0], 0.0), ([0, 1, 1], 0.0), ([0, 1, 2], 0.0),
       ([0, 2, 0], 0.0), ([0, 2, 1], 0.0), ([0, 2, 2], 0.0),
       ([1, 0, 0], 0.0), ([1, 0, 1], 0.0), ([1, 0, 2], 0.0),
       ([1, 1, 0], 0.0), ([1, 1, 1], 0.0), ([1, 1, 2], 0.0),
       ([1, 2, 0], 0.0), ([1, 2, 1], 0.0), ([1, 2, 2], 0.0),
       ([2, 0, 0], 4.08179211371555e-289), ([2, 0, 1], 0.0),
       ([2, 0, 2], 0.0), ([2, 1, 0], 1.0),
       ([2, 1, 1], 6.939504595227983e-225),
       ([2, 1, 2], 1.0626819127933375e-224),
       ([2, 2, 0], 1.1209583874093894e-260), ([2, 2, 1], 0.0),
       ([2, 2, 2], 0.0)], 
      dtype=[('strat', 'O'), ('weight', '<f8')])

... and this one can

array([ ([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], -2.40235968796357e-21),
       ([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], -3.3085614943210732e-21),
       ([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [2.0, 0.0, 0.0]], -1.2140117681585496e-23),
       ...,
       ([[2.0, 1.0, 0.0], [2.0, 1.0, 0.0], [2.0, 0.5, 0.5]], -1.154494002571172e-21),
       ([[2.0, 1.0, 0.0], [2.0, 1.0, 0.0], [2.0, 0.75, 0.25]], -1.3867815814699957e-22),
       ([[2.0, 1.0, 0.0], [2.0, 1.0, 0.0], [2.0, 1.0, 0.0]], 8.722432986511066e-20)], 
      dtype=[('strat', 'O'), ('weight', '<f8')])

The first one leaves the following trace:

In [111]: np.sort(aa, order=['weight', 'strat'])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-111-c3a3525a7694> in <module>()
----> 1 np.sort(aa, order=['weight', 'strat'])

/usr/lib/python3.4/site-packages/numpy/core/fromnumeric.py in sort(a, axis, kind, order)
    786     else:
    787         a = asanyarray(a).copy()
--> 788     a.sort(axis, kind, order)
    789     return a
    790 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


I should say that I asked this very question earlier today over at
#scipy, and jtaylor kindly run the offending array, and could not
reproduce the problem. I'm a bit stumped.

python --version: 3.4.1
numpy.__version__: 1.8.1
ipython --version: 2.1.0

Thanks for any advice. Cheers,
Manolo



More information about the NumPy-Discussion mailing list