How can I sort my dates in a numpy array in order?
Question:
I have this numpy array called all_periods:
array(['01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022',
'03/01/2020', '03/01/2021', '03/01/2022', '04/01/2020',
'04/01/2021', '04/01/2022', '05/01/2020', '05/01/2021',
'06/01/2020', '06/01/2021', '07/01/2020', '07/01/2021',
'08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021',
'12/01/2020', '12/01/2021'], dtype=object)
I want to sort this by day, month and year.
When I tried using the .sort_values function it only sorted it by the day. How do I sort it by day, month, year?
Answers:
This code is help you to sort by day,month and year:
import numpy as np
from datetime import datetime
all_periods = np.array(['01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020', '03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022', '05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020', '07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021', '10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020', '12/01/2021'])
dates = [datetime.strptime(d, '%d/%m/%Y') for d in all_periods]
sorted_dates = np.sort(dates)
sorted_periods = np.array([d.strftime('%d/%m/%Y') for d in sorted_dates])
print(sorted_periods)
Result:
['03/01/2020' '04/01/2020' '05/01/2020' '06/01/2020' '07/01/2020'
'08/01/2020' '09/01/2020' '10/01/2020' '11/01/2020' '12/01/2020'
'01/01/2021' '02/01/2021' '03/01/2021' '04/01/2021' '05/01/2021'
'06/01/2021' '07/01/2021' '08/01/2021' '09/01/2021' '10/01/2021'
'11/01/2021' '12/01/2021' '01/01/2022' '02/01/2022' '03/01/2022'
'04/01/2022']
I hope, it works for you
import numpy as np
from datetime import datetime
a = np.array([
'01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020',
'03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022',
'05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020',
'07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020',
'12/01/2021'
])
# sort by year, month and day
sorted(a, key=lambda x: datetime.strptime(x, '%d/%m/%Y'))
Output:
['03/01/2020', '04/01/2020', '05/01/2020', '06/01/2020', '07/01/2020', '08/01/2020', '09/01/2020', '10/01/2020', '11/01/2020', '12/01/2020', '01/01/2021', '02/01/2021', '03/01/2021', '04/01/2021', '05/01/2021', '06/01/2021', '07/01/2021', '08/01/2021', '09/01/2021', '10/01/2021', '11/01/2021', '12/01/2021', '01/01/2022', '02/01/2022', '03/01/2022', '04/01/2022']
You can simply define a sorting key function and sort the strings without any other library:
def func(x):
x = x.split('/')
return '.'.join(reversed(x))
all_periods = sorted(all_periods, key = func)
which gives
['03/01/2020', '04/01/2020', '05/01/2020', '06/01/2020', '07/01/2020', '08/01/2020', '09/01/2020', '10/01/2020', '11/01/2020', '12/01/2020', '01/01/2021', '02/01/2021', '03/01/2021', '04/01/2021', '05/01/2021', '06/01/2021', '07/01/2021', '08/01/2021', '09/01/2021', '10/01/2021', '11/01/2021', '12/01/2021', '01/01/2022', '02/01/2022', '03/01/2022', '04/01/2022']
I’d be inclined to convert them to numpy datetime64 objects and sort them directly, something like:
import numpy as np
from datetime import datetime
dates = np.array([
'01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020',
'03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022',
'05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020',
'07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020',
'12/01/2021'
])
conv_dates = np.array([
np.datetime64(
datetime.strptime(d, "%d/%m/%Y").strftime("%Y-%m-%d")
) for d in dates])
sorted_dates = np.sort(conv_dates)
I have this numpy array called all_periods:
array(['01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022',
'03/01/2020', '03/01/2021', '03/01/2022', '04/01/2020',
'04/01/2021', '04/01/2022', '05/01/2020', '05/01/2021',
'06/01/2020', '06/01/2021', '07/01/2020', '07/01/2021',
'08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021',
'12/01/2020', '12/01/2021'], dtype=object)
I want to sort this by day, month and year.
When I tried using the .sort_values function it only sorted it by the day. How do I sort it by day, month, year?
This code is help you to sort by day,month and year:
import numpy as np
from datetime import datetime
all_periods = np.array(['01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020', '03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022', '05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020', '07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021', '10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020', '12/01/2021'])
dates = [datetime.strptime(d, '%d/%m/%Y') for d in all_periods]
sorted_dates = np.sort(dates)
sorted_periods = np.array([d.strftime('%d/%m/%Y') for d in sorted_dates])
print(sorted_periods)
Result:
['03/01/2020' '04/01/2020' '05/01/2020' '06/01/2020' '07/01/2020'
'08/01/2020' '09/01/2020' '10/01/2020' '11/01/2020' '12/01/2020'
'01/01/2021' '02/01/2021' '03/01/2021' '04/01/2021' '05/01/2021'
'06/01/2021' '07/01/2021' '08/01/2021' '09/01/2021' '10/01/2021'
'11/01/2021' '12/01/2021' '01/01/2022' '02/01/2022' '03/01/2022'
'04/01/2022']
I hope, it works for you
import numpy as np
from datetime import datetime
a = np.array([
'01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020',
'03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022',
'05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020',
'07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020',
'12/01/2021'
])
# sort by year, month and day
sorted(a, key=lambda x: datetime.strptime(x, '%d/%m/%Y'))
Output:
['03/01/2020', '04/01/2020', '05/01/2020', '06/01/2020', '07/01/2020', '08/01/2020', '09/01/2020', '10/01/2020', '11/01/2020', '12/01/2020', '01/01/2021', '02/01/2021', '03/01/2021', '04/01/2021', '05/01/2021', '06/01/2021', '07/01/2021', '08/01/2021', '09/01/2021', '10/01/2021', '11/01/2021', '12/01/2021', '01/01/2022', '02/01/2022', '03/01/2022', '04/01/2022']
You can simply define a sorting key function and sort the strings without any other library:
def func(x):
x = x.split('/')
return '.'.join(reversed(x))
all_periods = sorted(all_periods, key = func)
which gives
['03/01/2020', '04/01/2020', '05/01/2020', '06/01/2020', '07/01/2020', '08/01/2020', '09/01/2020', '10/01/2020', '11/01/2020', '12/01/2020', '01/01/2021', '02/01/2021', '03/01/2021', '04/01/2021', '05/01/2021', '06/01/2021', '07/01/2021', '08/01/2021', '09/01/2021', '10/01/2021', '11/01/2021', '12/01/2021', '01/01/2022', '02/01/2022', '03/01/2022', '04/01/2022']
I’d be inclined to convert them to numpy datetime64 objects and sort them directly, something like:
import numpy as np
from datetime import datetime
dates = np.array([
'01/01/2021', '01/01/2022', '02/01/2021', '02/01/2022', '03/01/2020',
'03/01/2021', '03/01/2022', '04/01/2020', '04/01/2021', '04/01/2022',
'05/01/2020', '05/01/2021', '06/01/2020', '06/01/2021', '07/01/2020',
'07/01/2021', '08/01/2020', '08/01/2021', '09/01/2020', '09/01/2021',
'10/01/2020', '10/01/2021', '11/01/2020', '11/01/2021', '12/01/2020',
'12/01/2021'
])
conv_dates = np.array([
np.datetime64(
datetime.strptime(d, "%d/%m/%Y").strftime("%Y-%m-%d")
) for d in dates])
sorted_dates = np.sort(conv_dates)