[英]Converting a NumPy array of strings to datetime
I have an array of strings, for example我有一个字符串数组,例如
import numpy as np
foo = np.array( [b'2014-04-05', b'2014-04-06', b'2014-04-07'] )
To check for the data type of the array, I print it with要检查数组的数据类型,我打印它
print( foo.dtype )
which results in |S10
.这导致
|S10
。 Obviously, it consists of strings of length 10. I want to convert it into NumPy's datetime64
type.显然,它由长度为 10 的字符串组成。我想将其转换为 NumPy 的
datetime64
类型。
More precisely, I want to change the data type of the array without looping through a for-loop and copying it element-wise into a new array (the real array is actually very large).更准确地说,我想更改数组的数据类型,而不需要循环通过 for 循环并将其按元素复制到一个新数组中(实际数组实际上非常大)。 Naive as I am, I thought the following might work:
尽管我很天真,但我认为以下方法可能有效:
[ np.datetime64(x) for x in foo ]
Spoiler: it does not.剧透:它没有。 Printing the data type of the array results in the same output as before (ie,
|S10
).打印数组的数据类型会产生与之前相同的输出(即
|S10
)。
Is there a memory efficient way to convert the data type of the existing array without the necessity of copying everything to a new array?是否有一种内存有效的方法来转换现有数组的数据类型,而无需将所有内容复制到新数组中?
Use .astype
, with copy=False
to avoid creating a copy:使用
.astype
,并使用copy=False
以避免创建副本:
foo = np.array( [b'2014-04-05', b'2014-04-06', b'2014-04-07'] )
foo = foo.astype('datetime64',copy=False)
>>> foo
array(['2014-04-05', '2014-04-06', '2014-04-07'], dtype='datetime64[D]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.