I have an first array i containing datetime elements (with the shape (33416,)) and a second array m containing floats values (with the shape (33416,20)). This correspong to 20 measurements made at a specific instant.
I would like to sort i array according to increasing datetime values in i and sort lines of m array accordingly in order to have increasing instant of measurements and its corresponding 20 measurements at this instant.
Can some help me ? or understand my needs ???
You can also use numpy for this. Assuming you have:
dates = numpy.array([datetime(2012,02,03,12,00,00), datetime(2012,02,03,15,00,00), datetime(2012,02,03,13,00,00)])
values = numpy.array([[1, 1], [3, 3], [2, 2]])
You can do at first:
unified = numpy.concatenate((dates.reshape(len(dates), 1), values), axis=1)
This will merge your two list, which considering what you want should be a better suited way to hold your data anyway. So now you have:
unified = array([[2012-02-03 12:00:00, 1, 1],
[2012-02-03 15:00:00, 3, 3],
[2012-02-03 13:00:00, 2, 2]], dtype=object)
Now you can do:
unified = numpy.sort(unified, axis=0)
This will sort after date. Now if you still want just the separate lists you can get them with:
unified[:, 0]
>>> array([2012-02-03 12:00:00, 2012-02-03 13:00:00, 2012-02-03 15:00:00], dtype=object)
unified[:, 1:]
>>> array([[1, 1],
[2, 2],
[3, 3]], dtype=object)
EDIT after your comment Ok now that I fully understand what you want you can achieve that by replacing
unified = numpy.sort(unified, axis=0)
with:
unified = numpy.array(sorted(unified, key= lambda x: x[0]))
EDIT
have you tried what I just suggested? In my terminal:
unified = numpy.array([[datetime(2012,02,03,12,00,00), 4, 1],[datetime(2012,02,03,15,00,00), 5, 2],[datetime(2012,02,03,13,00,00), 2, 1]], dtype=object)
>>> unified
array([[2012-02-03 12:00:00, 4, 1],
[2012-02-03 15:00:00, 5, 2],
[2012-02-03 13:00:00, 2, 1]], dtype=object)
>>> unified = numpy.array(sorted(unified, key=lambda x: x[0]))
>>> unified
array([[2012-02-03 12:00:00, 4, 1],
[2012-02-03 13:00:00, 2, 1],
[2012-02-03 15:00:00, 5, 2]], dtype=object)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With