14

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 ValueError: Could not convert object to NumPy datetime. However, the following works as I intended

[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 can I convert my array into a format that conforms with Numpy's datetime64 function requirement?

I am using Numpy version 1.7.0. in python 3.4

Duna
  • 675
  • 2
  • 7
  • 14

2 Answers2

18

So 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 to be 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 convert back to numpy:

np.array(a,dtype=np.datetime64)
atomh33ls
  • 23,172
  • 18
  • 91
  • 149
  • 1
    np.array(a,dtype=np.datetime64) is great but it returns '17-10-2010T07:15:30', how to make it '17-10-2010 07:15:30' ? – Lisa Aug 17 '17 at 14:07
  • "*The to_datetime function in pandas seems to be more flexible*", but in that case, be aware of the [date/time nightmare in pandas](https://stackoverflow.com/a/46921593/774575) – mins Nov 10 '20 at 00:21
1

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

Anurag Dabas
  • 7,118
  • 4
  • 9
  • 28