我有一个日期矩阵我想按日期排序然后以相同的格式返回
data = np.array(
[[2015, 1, 1, 23, 4, 59],
[2015, 4, 30, 23, 5, 1],
[2015, 1, 1, 23, 5, 25],
[2015, 2, 15, 58,5, 0],
[2015, 5, 20, 50, 27, 37],
[2015, 6, 21, 25, 27, 29]])
我试了datetime。日期时间,但无法将数据转换回此格式
您可以在不转换为datetime的情况下对数据进行排序,因为日期/时间组件已经按排序顺序出现(年、月等)。所以np.sort(data, axis=0)
应该这样做:
import numpy as np
data = np.array(
[[2015, 1, 1, 23, 4, 59],
[2015, 4, 30, 23, 5, 1],
[2015, 1, 1, 23, 5, 25],
[2015, 2, 15, 58,5, 0],
[2015, 5, 20, 50, 27, 37],
[2015, 6, 21, 25, 27, 29]])
np.sort(data, axis=0)
array([[2015, 1, 1, 23, 4, 0],
[2015, 1, 1, 23, 5, 1],
[2015, 2, 15, 23, 5, 25],
[2015, 4, 20, 25, 5, 29],
[2015, 5, 21, 50, 27, 37],
[2015, 6, 30, 58, 27, 59]])