numpy.array.tolist() 将 numpy.datetime64 转换为 int

numpy.array.tolist() converts numpy.datetime64 to int

我有一个日期时间数组,需要将其转换为日期时间列表。我的数组如下所示:

import numpy as np

my_array = np.array(['2017-06-28T22:47:51.213500000', '2017-06-28T22:48:37.570900000',
                     '2017-06-28T22:49:46.736800000', '2017-06-28T22:50:41.866800000',
                     '2017-06-28T22:51:17.024100000', '2017-06-28T22:51:24.038300000'], dtype='datetime64[ns]')

my_list = my_array.tolist()

我需要一个日期时间值列表,但是当我这样做时 my_array.tolist(),我得到了一个数字时间戳列表:

[1498690071213500000,
 1498690117570900000,
 1498690186736800000,
 1498690241866800000,
 1498690277024100000,
 1498690284038300000]

我的问题是如何在从数组转换为列表时保留日期时间格式,或者如何将时间戳列表转换为列表日期时间值?

尝试

# convert to string type first
my_list = my_array.astype(str).tolist()
my_list
# ['2017-06-28T22:47:51.213500000', '2017-06-28T22:48:37.570900000', '2017-06-28T22:49:46.736800000', '2017-06-28T22:50:41.866800000', '2017-06-28T22:51:17.024100000', '2017-06-28T22:51:24.038300000']

其他答案提供了更直接的方法,但为了完整起见,您可以循环调用 datetime.datetime.fromtimestamp

from datetime import datetime
[datetime.fromtimestamp(x) for x in my_array.astype(object)/1e9]

#[datetime.datetime(2017, 6, 28, 15, 47, 51, 213500),
# datetime.datetime(2017, 6, 28, 15, 48, 37, 570900),
# datetime.datetime(2017, 6, 28, 15, 49, 46, 736800),
# datetime.datetime(2017, 6, 28, 15, 50, 41, 866800),
# datetime.datetime(2017, 6, 28, 15, 51, 17, 24100),
# datetime.datetime(2017, 6, 28, 15, 51, 24, 38300)]

numpy.ndarray 显式转换为原生 Python list 会将内容保留为 numpy.datetime64 对象:

>>> list(my_array)
[numpy.datetime64('2017-06-28T22:47:51.213500000'),
 numpy.datetime64('2017-06-28T22:48:37.570900000'),
 numpy.datetime64('2017-06-28T22:49:46.736800000'),
 numpy.datetime64('2017-06-28T22:50:41.866800000'),
 numpy.datetime64('2017-06-28T22:51:17.024100000'),
 numpy.datetime64('2017-06-28T22:51:24.038300000')]

但是,如果您想从整数时间戳返回到 numpy.datetime64 对象,numpy.ndarray.tolist 此处给出的数字以纳秒格式给出,因此您也可以使用列表理解像下面这样:

>>> [np.datetime64(x, "ns") for x in my_list]
[numpy.datetime64('2017-06-28T22:47:51.213500000'),
 numpy.datetime64('2017-06-28T22:48:37.570900000'),
 numpy.datetime64('2017-06-28T22:49:46.736800000'),
 numpy.datetime64('2017-06-28T22:50:41.866800000'),
 numpy.datetime64('2017-06-28T22:51:17.024100000'),
 numpy.datetime64('2017-06-28T22:51:24.038300000')]

并且如果您希望最终结果作为 Python datetime.datetime 对象而不是 numpy.datetime64 对象,(根据当地需要进行调整):

>>> from datetime import datetime
>>> list(map(datetime.utcfromtimestamp, my_array.astype(np.uint64) / 1e9))
[datetime.datetime(2017, 6, 28, 22, 47, 51, 213500),
 datetime.datetime(2017, 6, 28, 22, 48, 37, 570900),
 datetime.datetime(2017, 6, 28, 22, 49, 46, 736800),
 datetime.datetime(2017, 6, 28, 22, 50, 41, 866800),
 datetime.datetime(2017, 6, 28, 22, 51, 17, 24100),
 datetime.datetime(2017, 6, 28, 22, 51, 24, 38300)]

编辑: 提供了一种更直接的方法来从 numpy.datetime64[ns] 数组到 Python [=18= 的列表] 对象比此处描述的要多。

NumPy 无法将 'datetime64[ns]' 实例转换为 Python datetime.datetime 实例,因为 datetime 实例不支持纳秒分辨率。

如果您将数组转换为 'datetime64[us]',那么时间戳只有微秒分辨率,那么 .tolist() 方法将为您提供 datetime.datetime 个实例:

In [25]: my_array
Out[25]: 
array(['2017-06-28T22:47:51.213500000', '2017-06-28T22:48:37.570900000',
       '2017-06-28T22:49:46.736800000', '2017-06-28T22:50:41.866800000',
       '2017-06-28T22:51:17.024100000', '2017-06-28T22:51:24.038300000'],
      dtype='datetime64[ns]')

In [26]: my_array.astype('datetime64[us]').tolist()
Out[26]: 
[datetime.datetime(2017, 6, 28, 22, 47, 51, 213500),
 datetime.datetime(2017, 6, 28, 22, 48, 37, 570900),
 datetime.datetime(2017, 6, 28, 22, 49, 46, 736800),
 datetime.datetime(2017, 6, 28, 22, 50, 41, 866800),
 datetime.datetime(2017, 6, 28, 22, 51, 17, 24100),
 datetime.datetime(2017, 6, 28, 22, 51, 24, 38300)]