我有一个2D形式的日期:
[Y Y Y ... ]
[M M M ... ]
[D D D ... ]
[H H H ... ]
[M M M ... ]
[S S S ... ]
所以它看起来像
data = np.array([
[2015,2015,2015],# ...
[ 1,1,1],[ 1,2,2],[ 23,23,0],[ 4,5,37,37],[ 59,25,27,29]
])
将此转换为一个datetime对象列表的最佳方法是什么?
最佳答案
import datetime
import numpy as np
data = np.array(
[[2015,29]]
)
# Transpose the data so that columns become rows.
data = data.T
# A simple list comprehension does the trick,'*' making sure
# the values are unpacked for 'datetime.datetime'.
new_data = [datetime.datetime(*x) for x in data]
print(new_data)
[datetime.datetime(2015,4,59),datetime.datetime(2015,
1,1),2),
datetime.datetime(2015,25),
2,27),29)]