4

I have two numpy arrays: date_month of the form

array([datetime.datetime(2013, 1, 1, 12, 0),
       datetime.datetime(2013, 9, 1, 12, 0),
       datetime.datetime(2013, 5, 1, 12, 0)], dtype=object)

and emission_each_month of the form

array([5,7,3])

The entry 5 of emission_each_month belongs to the timestamp (2013, 1, 1, 12, 0), 7 belongs to (2013, 9, 1, 12, 0), 3 belongs to (2013, 5, 1, 12, 0). (In reality my data is much bigger)

I would like to have my data sorted by date. How do I that?

1 Answer 1

3

You can use numpy.argsort() to get the indexes of the sorted array of datetime object , and then use the returned indices to sort the array - emission_each_month . Example -

In [66]: import datetime

In [67]: import numpy as np

In [68]: n = np.array([5,7,3])

In [69]: d = np.array([datetime.datetime(2013, 1, 1, 12, 0), datetime.datetime(2013, 9, 1, 12, 0), datetime.datetime(2013, 5, 1, 12, 0)])

In [72]: n[np.argsort(d)]
Out[72]: array([5, 3, 7])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.