python numpy -将时间戳转换为日期时间

sycxhyv7  于 2023-10-19  发布在  Python
关注(0)|答案(3)|浏览(138)

我有一个np数组,如下所示:

example = np.array([[Timestamp('2005-03-06 17:00:00'), 1225.75, 1226.25, 1225.0, 1225.5,
        1668.0],
       [Timestamp('2005-03-06 17:30:00'), 1225.75, 1227.5, 1225.75, 1227.0,
        1603.0],
       [Timestamp('2005-03-06 18:00:00'), 1227.0, 1227.5, 1226.75, 1227.25,
        590.0]], dtype=object)

第一列是时间戳类型值。如何将这些值转换为日期时间?我知道在这个主题上很少有类似的问题,但我无法对它形成清晰的理解,并在此基础上找出一个干净整洁的解决方案。
我可以用example[0,0].to_datetime()转换单个值的时间戳,但如何一次转换所有时间戳?最好是example[:,0]. ...

eivgtgni

eivgtgni1#

如果我将Timestamp定义为numpy datetime dtype:

In [43]: Timestamp=np.datetime64

然后我可以复制粘贴您的example

In [44]: example = np.array([[Timestamp('2005-03-06 17:00:00'), 1225.75, 1226.25 , 1225.0, 1225.5, 1668.0],
    ...:        [Timestamp('2005-03-06 17:30:00'), 1225.75, 1227.5, 1225.75, 1227.0,         1603.0],
    ...:        [Timestamp('2005-03-06 18:00:00'), 1227.0, 1227.5, 1226.75, 1227.25, 590.0]], dtype=object)

请注意,此数组为dtypeobject

In [45]: example
Out[45]: 
array([[numpy.datetime64('2005-03-06T17:00:00'), 1225.75, 1226.25, 1225.0,1225.5, 1668.0],
       [numpy.datetime64('2005-03-06T17:30:00'), 1225.75, 1227.5, 1225.75, 1227.0, 1603.0],
       [numpy.datetime64('2005-03-06T18:00:00'), 1227.0, 1227.5, 1226.75, 1227.25, 590.0]], dtype=object)

第一列是:

In [46]: example[:,0]
Out[46]: 
array([numpy.datetime64('2005-03-06T17:00:00'),
       numpy.datetime64('2005-03-06T17:30:00'),
       numpy.datetime64('2005-03-06T18:00:00')], dtype=object)

它可以转换为datetime64元素的数组:

In [47]: example[:,0].astype(np.datetime64)
Out[47]: array(['2005-03-06T17:00:00', '2005-03-06T17:30:00', '2005-03-06T18:00:00'], dtype='datetime64[s]')

tolist对于这种类型的数组,将元素转换为datetime对象:

In [48]: example[:,0].astype(np.datetime64).tolist()
Out[48]: 
[datetime.datetime(2005, 3, 6, 17, 0),
 datetime.datetime(2005, 3, 6, 17, 30),
 datetime.datetime(2005, 3, 6, 18, 0)]

或者,抓取pandas.Timestamp函数

In [50]: Timestamp = pd.Timestamp

In [52]: example
Out[52]: 
array([[Timestamp('2005-03-06 17:00:00'), 1225.75, 1226.25, 1225.0, 1225.5, 1668.0],
       [Timestamp('2005-03-06 17:30:00'), 1225.75, 1227.5, 1225.75, 1227.0,  1603.0],
       [Timestamp('2005-03-06 18:00:00'), 1227.0, 1227.5, 1226.75, 1227.25,  590.0]], dtype=object)

In [64]: ts = example[:,0]
In [65]: ts
Out[65]: 
array([Timestamp('2005-03-06 17:00:00'), Timestamp('2005-03-06 17:30:00'), Timestamp('2005-03-06 18:00:00')], dtype=object)

Timestamp对象的迭代转换

In [67]: np.array([t.to_datetime() for t in ts])
Out[67]: 
array([datetime.datetime(2005, 3, 6, 17, 0),
       datetime.datetime(2005, 3, 6, 17, 30),
       datetime.datetime(2005, 3, 6, 18, 0)], dtype=object)

但我发现astype可以与Timestamp对象一起工作:

In [73]: ts = example[:,0]
In [74]: ts.astype('datetime64[s]')
Out[74]: array(['2005-03-06T17:00:00', '2005-03-06T17:30:00', '2005-03-06T18:00:00'], dtype='datetime64[s]')

所以我可以使用tolist在一行中完成转换:

In [75]: ts.astype('datetime64[s]').tolist()
Out[75]: 
[datetime.datetime(2005, 3, 6, 17, 0),
 datetime.datetime(2005, 3, 6, 17, 30),
 datetime.datetime(2005, 3, 6, 18, 0)]

我不会将其描述为最终解决方案,但它可以让您了解numpy如何处理日期。
对于数组数学,我坚持使用datetime64 dtype。要将example[:,1:]浮点数沿着放在一个数组中,必须使用结构化数组。

尝试一个副本:

In [80]: ex1 = example.copy()

In [82]: ex1[:,0] = example[:,0].astype('datetime64[s]').tolist()
In [83]: ex1
Out[83]: 
array([[datetime.datetime(2005, 3, 6, 17, 0), 1225.75, 1226.25, 1225.0, 1225.5, 1668.0],
       [datetime.datetime(2005, 3, 6, 17, 30), 1225.75, 1227.5, 1225.75, 1227.0, 1603.0],
       [datetime.datetime(2005, 3, 6, 18, 0), 1227.0, 1227.5, 1226.75, 1227.25, 590.0]], 
      dtype=object)
k2arahey

k2arahey2#

西洛,试试:

example[:,0]= map(lambda x: x.to_datetime(), example[:,0])
svgewumm

svgewumm3#

很简单:
t = np.datetime64('2018-08-18 23:25') --> numpy.datetime64('2018-06-18T23:31')
str(t) --> '2018-06-18T23:31'
t.tolist() --> datetime.datetime(2018, 6, 18, 23, 31)
这就是你所需要的

相关问题