在numpy(python)中将datetime字符串转换为datetime

Dun*_*una 10 datetime numpy python-3.x python-datetime pandas

我想转换

['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]
Run Code Online (Sandbox Code Playgroud)

进入Numpy日期时间对象.

import numpy as np
[np.datetime64(x) for x in ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]] 
Run Code Online (Sandbox Code Playgroud)

提出来ValueError: Could not convert object to NumPy datetime.但是,以下按照我的意图工作

[np.datetime64(x) for x in ['2010-10-17 07:15:30', '2011-05-13 08:20:35', "2012-01-15 09:09:09"]] 
Run Code Online (Sandbox Code Playgroud)

我怎样才能将我的数组,与符合格式Numpydatetime64功能要求?

我使用Numpy版本1.7.0.在python 3.4中

ato*_*3ls 11

据我所知,np.datetime64只适用于

ISO 8601日期或日期时间格式的字符串

to_datetime在功能上pandas似乎更加灵活:

import pandas as pd
a=pd.to_datetime(['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"])
Run Code Online (Sandbox Code Playgroud)

当然你可以轻松转换回numpy:

np.array(a,dtype=np.datetime64)
Run Code Online (Sandbox Code Playgroud)