简体   繁体   English

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

[英]python numpy - convert Timestamps to datetime

I have a np array such as follow: 我有一个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)

The first column is a Timestamp type value. 第一列是时间戳记类型值。 How do i convert those values to datetime? 如何将这些值转换为日期时间? I know there are few similar questions on the topic, but i couldnt manage to form a clear understanding of it and figure out a clean neat solution based on them. 我知道在该主题上很少有类似的问题,但是我无法对此形成清晰的理解,并根据它们得出一种干净利落的解决方案。

I can convert the timestamp of a single value with example[0,0].to_datetime() but how to do it on all the Timestamps at once? 我可以使用example[0,0].to_datetime()转换单个值的时间戳,但是如何一次在所有时间戳上转换呢? ideally something like example[:,0]. 理想情况下,例如example[:,0]. ... ...

If I define Timestamp as the numpy datetime dtype: 如果我将Timestamp定义为numpy datetime dtype:

In [43]: Timestamp=np.datetime64

Then I can copy-n-paste your example : 然后,我可以复制粘贴您的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)

Note that this array is dtype object 请注意,此数组是dtype object

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)

The 1st column is: 第一列是:

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)

which can be converted to an array of datetime64 elements: 可以将其转换为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 for this type of array converts the elements to datetime objects: 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)]

Alternatively, grabing the pandas.Timestamp function 或者,获取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)

Iterative conversion of the Timestamp objects 时间戳对象的迭代转换

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)

But I discovered that astype works with Timestamp objects: 但是我发现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]')

So I can use that tolist to do the conversion in one line: 因此,我可以使用该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)]

I wouldn't describe this as a final solution, but it gives you an idea of how numpy deals with dates. 我不会将其描述为最终解决方案,但是它使您了解numpy如何处理日期。

For array math I'd stick with the datetime64 dtype. 对于数组数学,我会坚持使用datetime64 dtype。 To keep in one array along with the example[:,1:] floats you have to use a structured array. 要与example[:,1:]浮点数一起保留在一个数组中example[:,1:]您必须使用结构化数组。

================= =================

Experimenting with a copy: 试用副本:

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)

Helo,尝试:

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

It's quite straightforward: 这很简单:

t = np.datetime64('2018-08-18 23:25') --> numpy.datetime64('2018-06-18T23:31') t = np.datetime64('2018-08-18 23:25') -> numpy.datetime64('2018-06-18T23:31')

t.\\_\\_str\\_\\_() --> '2018-06-18T23:31' t.\\_\\_str\\_\\_() -> '2018-06-18T23:31'

t.tolist() --> datetime.datetime(2018, 6, 18, 23, 31) t.tolist() -> datetime.datetime(2018, 6, 18, 23, 31) t.tolist() datetime.datetime(2018, 6, 18, 23, 31)

This will be all you need. 这就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM