Convert datetime string to datetime in numpy (python)

I would like to convert

['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"] 

into a Numpy datetime object.

 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"]] 

raised a ValueError: Could not convert object to NumPy datetime . However, the following works as I thought

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

How to convert an array to a format that meets the requirements of Numpy datetime64 ?

I am using Numpy version 1.7.0. in python 3.4

+5
source share
1 answer

As far as I can tell, np.datetime64 only works with

strings in ISO 8601 date or datetime format

The to_datetime function in pandas seems more flexible:

 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"]) 

Of course, you can easily return to numpy :

 np.array(a,dtype=np.datetime64) 
+5
source

Source: https://habr.com/ru/post/1207572/


All Articles