[英]How to convert numpy array from numpy.int64 to datetime?
I have the following array of type <class 'numpy.ndarray'>
我有以下类型为
<class 'numpy.ndarray'>
数组
array([20181010, 20181031, 20181116, 20181012, 20181005, 20181008,
20181130, 20181011, 20181005, 20181116])
How can I convert its constituents from the current type <class 'numpy.int64'>
to datetime in numpy
? 如何将其成分从当前类型
<class 'numpy.int64'>
转换为numpy
datetime? I want to find a quick way and my understanding is that using a loop or list comprehension, as well as converting this numpy.array
to pandas
or to a list
will be slower. 我想找到一种快速的方法,我的理解是,使用循环或列表理解以及将此
numpy.array
转换为pandas
或list
都将比较慢。
Please correct me if I am wrong. 如果我错了,请纠正我。
PS This question may have been answered somewhere, but I could not find a single solution which works. PS这个问题可能在某个地方已经回答过,但是我找不到一个可行的解决方案。
pandas
has a better concept of what can be considered a date: pandas
对日期可以有更好的理解:
import numpy as np
import pandas as pd
arr = np.array([20181010, 20181031, 20181116, 20181012, 20181005,
20181008, 20181130, 20181011, 20181005, 20181116])
pd.to_datetime(arr.astype(str)).values
Running this over a set of 10,000,000 entries: 在一组10,000,000个条目上运行此命令:
%%prun import numpy as np; import pandas as pd
lst = [20181010, 20181031, 20181116, 20181012, 20181005,
20181008, 20181130, 20181011, 20181005, 20181116]*1000000
arr = np.array(lst)
arr_str = arr.astype(str)
pd.to_datetime(arr_str).values
produces a prun
of 产生
prun
ncalls tottime percall cumtime percall filename:lineno(function)
1 8.977 8.977 8.977 8.977 {method 'astype' of 'numpy.ndarray' objects}
1 4.394 4.394 4.394 4.394 {built-in method pandas._libs.tslib.array_to_datetime}
2 2.344 1.172 2.344 1.172 {built-in method pandas._libs.algos.ensure_object}
4 0.918 0.229 0.918 0.229 {built-in method numpy.core.multiarray.array}
1 0.313 0.313 7.053 7.053 datetimes.py:106(to_datetime)
...
It's efficient enough. 它足够有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.