如何在Python中将这两个数组转换为日期时间对象?

时间:2016-05-24 18:17:36

标签: python arrays datetime

我有两列数组(日期和时间)彼此相邻,我需要将它们转换为datetime对象,以便我可以使用它们在x轴上绘图。这些数据不断更新,因此它们不是设定数字,只是fyi。

datetime = np.column_stack((date,time))
datetime = np.array(datetime)
datetime
    array([[b'143.0', b'08:15'],
       [b'143.0', b'08:30'],
       [b'143.0', b'08:45'],
       [b'143.0', b'09:00'],
       [b'143.0', b'09:15'],
       [b'143.0', b'09:30'],
       [b'143.0', b'09:45'],
       [b'143.0', b'10:00'],
       [b'143.0', b'10:15'],
       [b'143.0', b'10:30'],
       [b'143.0', b'10:45'],
       [b'143.0', b'11:00'],
       [b'143.0', b'11:15'],
       [b'143.0', b'11:30'],
       [b'143.0', b'11:45'],
       [b'143.0', b'12:00'],
       [b'143.0', b'12:15'],
       [b'143.0', b'12:30'],
       [b'143.0', b'12:45'],
       [b'143.0', b'13:00'],
       [b'143.0', b'13:15'],
       [b'143.0', b'13:30'],
       [b'143.0', b'13:45'],
       [b'143.0', b'14:00'],
       [b'143.0', b'14:15'],
       [b'143.0', b'14:30'],
       [b'143.0', b'14:45'],
       [b'143.0', b'15:00'],
       [b'143.0', b'15:15'],
       [b'143.0', b'15:30'],
       [b'143.0', b'15:45'],
       [b'143.0', b'16:00'],
       [b'143.0', b'16:15'],
       [b'143.0', b'16:30'],
       [b'143.0', b'16:45'],
       [b'143.0', b'17:00'],
       [b'143.0', b'17:15'],
       [b'143.0', b'17:30'],
       [b'143.0', b'17:45'],
       [b'143.0', b'18:00'],
       [b'143.0', b'18:15'],
       [b'143.0', b'18:30'],
       [b'143.0', b'18:45'],
       [b'143.0', b'19:00'],
       [b'143.0', b'19:15'],
       [b'143.0', b'19:30'],
       [b'143.0', b'19:45'],
       [b'143.0', b'20:00'],
       [b'143.0', b'20:15'],
       [b'143.0', b'20:30'],
       [b'143.0', b'20:45'],
       [b'143.0', b'21:00'],
       [b'143.0', b'21:15'],
       [b'143.0', b'21:30'],
       [b'143.0', b'21:45'],
       [b'143.0', b'22:00'],
       [b'143.0', b'22:15'],
       [b'143.0', b'22:30'],
       [b'143.0', b'22:45'],
       [b'143.0', b'23:00'],
       [b'143.0', b'23:15'],
       [b'143.0', b'23:30'],
       [b'143.0', b'23:45'],
       [b'144.0', b'00:00'],
       [b'144.0', b'00:15'],
       [b'144.0', b'00:30'],
       [b'144.0', b'00:45'],
       [b'144.0', b'01:00'],
       [b'144.0', b'01:15'],
       [b'144.0', b'01:30'],
       [b'144.0', b'01:45'],
       [b'144.0', b'02:00'],
       [b'144.0', b'02:15'],
       [b'144.0', b'02:30'],
       [b'144.0', b'02:45'],
       [b'144.0', b'03:00'],
       [b'144.0', b'03:15'],
       [b'144.0', b'03:30'],
       [b'144.0', b'03:45'],
       [b'144.0', b'04:00'],
       [b'144.0', b'04:15'],
       [b'144.0', b'04:30'],
       [b'144.0', b'04:45'],
       [b'144.0', b'05:00'],
       [b'144.0', b'05:15'],
       [b'144.0', b'05:30'],
       [b'144.0', b'05:45'],
       [b'144.0', b'06:00'],
       [b'144.0', b'06:15'],
       [b'144.0', b'06:30'],
       [b'144.0', b'06:45'],
       [b'144.0', b'07:00'],
       [b'144.0', b'07:15'],
       [b'144.0', b'07:30'],
       [b'144.0', b'07:45'],
       [b'144.0', b'08:00'],
       [b'144.0', b'08:15'],
       [b'144.0', b'08:30'],
       [b'144.0', b'08:45'],
       [b'144.0', b'09:00'],
       [b'144.0', b'09:15'],
       [b'144.0', b'09:30'],
       [b'144.0', b'09:45'],
       [b'144.0', b'10:00'],
       [b'144.0', b'10:15'],
       [b'144.0', b'10:30'],
       [b'144.0', b'10:45'],
       [b'144.0', b'11:00'],
       [b'144.0', b'11:15'],
       [b'144.0', b'11:30'],
       [b'144.0', b'11:45'],
       [b'144.0', b'12:00'],
       [b'144.0', b'12:15'],
       [b'144.0', b'12:30'],
       [b'144.0', b'12:45'],
       [b'144.0', b'13:00'],
       [b'144.0', b'13:15'],
       [b'144.0', b'13:30'],
       [b'144.0', b'13:45'],
       [b'144.0', b'14:00'],
       [b'144.0', b'14:15'],
       [b'144.0', b'14:30'],
       [b'144.0', b'14:45'],
       [b'144.0', b'15:00'],
       [b'144.0', b'15:15'],
       [b'144.0', b'15:30'],
       [b'144.0', b'15:45'],
       [b'144.0', b'16:00'],
       [b'144.0', b'16:15'],
       [b'144.0', b'16:30'],
       [b'144.0', b'16:45'],
       [b'144.0', b'17:00'],
       [b'144.0', b'17:15'],
       [b'144.0', b'17:30'],
       [b'144.0', b'17:45'],
       [b'144.0', b'18:00'],
       [b'144.0', b'18:15'],
       [b'144.0', b'18:30'],
       [b'144.0', b'18:45'],
       [b'144.0', b'19:00'],
       [b'144.0', b'19:15'],
       [b'144.0', b'19:30'],
       [b'144.0', b'19:45'],
       [b'144.0', b'20:00'],
       [b'144.0', b'20:15'],
       [b'144.0', b'20:30'],
       [b'144.0', b'20:45'],
       [b'144.0', b'21:00'],
       [b'144.0', b'21:15'],
       [b'144.0', b'21:30'],
       [b'144.0', b'21:45'],
       [b'144.0', b'22:00'],
       [b'144.0', b'22:15'],
       [b'144.0', b'22:30'],
       [b'144.0', b'22:45'],
       [b'144.0', b'23:00'],
       [b'144.0', b'23:15'],
       [b'144.0', b'23:30'],
       [b'144.0', b'23:45'],
       [b'145.0', b'00:00'],
       [b'145.0', b'00:15'],
       [b'145.0', b'00:30'],
       [b'145.0', b'00:45'],
       [b'145.0', b'01:00'],
       [b'145.0', b'01:15'],
       [b'145.0', b'01:30'],
       [b'145.0', b'01:45'],
       [b'145.0', b'02:00'],
       [b'145.0', b'02:15'],
       [b'145.0', b'02:30'],
       [b'145.0', b'02:45'],
       [b'145.0', b'03:00'],
       [b'145.0', b'03:15'],
       [b'145.0', b'03:30'],
       [b'145.0', b'03:45'],
       [b'145.0', b'04:00'],
       [b'145.0', b'04:15'],
       [b'145.0', b'04:30'],
       [b'145.0', b'04:45'],
       [b'145.0', b'05:00'],
       [b'145.0', b'05:15'],
       [b'145.0', b'05:30'],
       [b'145.0', b'05:45'],
       [b'145.0', b'06:00'],
       [b'145.0', b'06:15'],
       [b'145.0', b'06:30'],
       [b'145.0', b'06:45'],
       [b'145.0', b'07:00'],
       [b'145.0', b'07:15'],
       [b'145.0', b'07:30'],
       [b'145.0', b'07:45'],
       [b'145.0', b'08:00']], 
      dtype='|S32')

第一栏是朱利安时间,所以它基本上是今天日期之后的最后3天,因为它更新包括最近3天。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以首先使用列表解析将数据重新整形为与numpy的datetime64类型兼容的格式,然后从中创建一个数组。对于普通的datetime.datetime来说,它看起来不像numpy。

不完全确定朱利安的时间是如何运作的,但我假设你的第一列是从一年开始的一天偏移。这应该可以得到你想要的东西。

from datetime import datetime, timedelta
data = [[b'143.0', b'08:15'], [b'143.0', b'08:30']...]

base = datetime(2016, 1, 1, 0, 0, 0, 0)
transformed = [(base + timedelta(days=int(float(x)), hours=int(y[0:2]), minutes=int(y[3:5]))).isoformat() for x, y in data]

my_array = np.array(transformed, dtype='datetime64')