0

I have built a recarray with np.rec.fromarrays and following structure :

dtype=np.dtype([('layers', 'U256'), ('hours', datetime.datetime), ('points', 'U256')]))

I get an object like this :

[('image1.jpg', datetime.datetime(1900, 1, 1, 21, 20), 'mypoints.points')
 ('image2.jpg', datetime.datetime(1900, 1, 1, 21, 15), 'mypoints.points')]

with recarray type. I want to sort my recarray based on the second column containing datetime. I tried numpy.recarray.sort but it returns a NoneType object. I use it like this :

mytable.sort(order='hours')

I also tried to pass kind='quicksort' to the function but doesn't understand its usefulness.

dmjf08
  • 143
  • 7

1 Answers1

1

I tried to reproduce your data

x1=np.array(['image1.jpg', 'image2.jpg'])
x2=np.array([datetime.datetime(1900, 1, 1, 21, 10), datetime.datetime(1900, 1, 1, 21, 9)])
x3=np.array(['mypoints.points', 'mypoints.points'])
    
array = np.rec.fromarrays([x1, x2, x3], dtype=np.dtype([('layers', 'U256'), ('hours', datetime.datetime), ('points', 'U256')]))

Output:

rec.array([('image1.jpg', datetime.datetime(1900, 1, 1, 21, 20), 'mypoints.points'),
           ('image2.jpg', datetime.datetime(1900, 1, 1, 21, 15), 'mypoints.points')],
          dtype=[('layers', '<U256'), ('hours', 'O'), ('points', '<U256')])

But was not able to get same error... array.sort(order='hours') works fine

rec.array([('image2.jpg', datetime.datetime(1900, 1, 1, 21, 15), 'mypoints.points'),
           ('image1.jpg', datetime.datetime(1900, 1, 1, 21, 20), 'mypoints.points')],
          dtype=[('layers', '<U256'), ('hours', 'O'), ('points', '<U256')])
Stanislav D.
  • 110
  • 1
  • 6
  • I copied the exact same code than yours and I get a None object for `array.sort(order='hours')`... what version of numpy are you using ? – dmjf08 May 09 '22 at 14:46
  • My mistake, I didn't see the output type of `recarray.sort`which is None. The function modifies the object itself... – dmjf08 May 09 '22 at 14:55