np.datetime64
works with format yyyy-mm-dd hh:mm:ss
use to_datetime
function in pandas
as it is more flexible
or if you just want to use np.datetime64
then:-
if you have a list of 5-6 elements you can directly make use of np.datetime64
data type by just changing the format(yyyy-mm-dd hh:mm:ss)
of date in your list
for example:-
dates=['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]
to
dates=['2010-10-17 07:15:30', '2011-05-13 08:20:35', "2013-01-15 09:09:09"]
then create array
by np.array(dates,dtype=np.datetime64)
.
.
.
.
if you have a list of more than 6 elements then follow the process written below:-
suppose you have a list of more than 6 elements:-
dates=['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09","25-05-2013 10:09:09","28-08-2013 06:19:09","30-09-2014 04:13:49",.......]
firstly we have to split the value inside the list:-
splited=[date.split('-') for date in dates]
then we have to split year and timestamp from splited so to do this:-
year_time=[val[2].split(' ') for val in splited]
now we have to store the value of year and time in 2 different lists:-
year=[]
time=[]
for x in year_time:
year.append(x[0])
time.append(x[1])
now we have to join month and date:-
date_month=[]
for index in [x[0:2] for x in splited]:
date_month.append('-'.join([index[1],index[0]]))
now we have to join year value:-
date_month_year=[]
count=0
for x in year:
date_month_year.append('-'.join([x,date_month[count]]))
count=count+1
now we have to join time value:-
date_month_year_time=[]
count=0
for x in time:
date_month_year_time.append(' '.join([date_month_year[count],x]))
count=count+1
now finally convert your final list to array:-
import numpy as np
dates=np.array(date_month_year_time,dtype=np.datetime64)
i am attaching the link of ipynb file just have a look if you have any doubt or need reference:-
https://drive.google.com/file/d/1FEWWFtQb3rk9A49NJaVmkjg1kX9Kyxtr/view?usp=sharing