python 我如何在一个numpy数组中按顺序排列我的日期?

nfeuvbwi  于 2023-02-18  发布在  Python
关注(0)|答案(4)|浏览(166)

我有一个名为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)

我想按日、月、年把这个分类。
当我尝试使用.sort_values函数时,它只按天排序,我如何按天,月,年排序呢?

xesrikrc

xesrikrc1#

这段代码可以帮助你按日、月和年排序:

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)
    • 结果:**
['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']
khbbv19g

khbbv19g2#

您可以简单地定义一个排序键函数并对字符串进行排序,而无需任何其他库:

def func(x):
    x = x.split('/')
    return '.'.join(reversed(x))
    
all_periods = sorted(all_periods, key = func)

它给出了

['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']
anauzrmj

anauzrmj3#

希望对你有用

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'))
    • 输出:**
['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']
mkshixfv

mkshixfv4#

我倾向于将它们转换为numpy datetime64对象并直接排序,类似于:

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)

相关问题