1

I know that we can create a single string to np.datetime64 format such as:

a = np.datetime64('2020-01-01')

But what if we have a list with multiple strings of dates in it?

How are we able to apply the same np.datetime64 to convert all the elements inside into a datetime format? Apart from doing a for-loop perhaps.

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
funkymickey
  • 111
  • 3
  • 8
  • Does this answer your question? [How can I make a python numpy arange of datetime](https://stackoverflow.com/questions/12137277/how-can-i-make-a-python-numpy-arange-of-datetime) – sushanth Aug 25 '20 at 09:22

2 Answers2

2

When you have your string list, use it as a source to a Numpy array, passing datetime64 as dtype. E.g.:

lst = ['2020-01-01', '2020-02-05', '2020-03-07' ]
a = np.array(lst, dtype='datetime64')

When you execute a (actually print this array in a notebook), you will get:

array(['2020-01-01', '2020-02-05', '2020-03-07'], dtype='datetime64[D]')

As you can see, in this case the default precision is Day. But you can pass the precision explicitely, e.g. b = np.array(lst, dtype='datetime64[s]').

Don't be misled by apostrophes surrounding each element in the above printout, they are not strings. To check it, execute a[0] and you will get:

numpy.datetime64('2020-01-01')
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Hmm I am not very sure what do you mean about the precision, I ran the code a = np.array(lst, dtype='datetime64') and printed out each element type() and it gave me datetime64 indeed. – funkymickey Aug 26 '20 at 14:10
  • Date precision can be e.g. "Day", but only in *Numpy*. *Pandas* uses *ms* (miliseconds) precision. – Valdi_Bo Aug 26 '20 at 14:12
  • Meaning in Numpy I am unable to print the seconds out ? Day is the most precised dimension ? – funkymickey Aug 26 '20 at 14:24
  • How will the format be like for each string: 'YYYY-MM-DD' that is all I know hehe – funkymickey Aug 26 '20 at 14:25
  • Create *Numpy* arrays of *datetime* type, but with different precision and then print values. E.g. for *Day* precision you can have values only at 0:00 hrs in various days. – Valdi_Bo Aug 26 '20 at 14:27
  • Hmm gotcha. But let's say if I want to just convert a string with just Y-M-D precision and I just pass the list into the array function including 'dtype = datetime64', I will successfully converted each element in the array to datetime64 yep? – funkymickey Aug 26 '20 at 14:31
0

Using list comprehension:

strings_list= [...]
npdate_list = [np.datetime64(x) for x in strings_list]

Is there a specific reason for you to want to avoid a loop?

List comprehension is okay?

kwra
  • 16
  • OP is looking for solution without loops. – sushanth Aug 25 '20 at 09:29
  • Yes, I read that. Waiting to see why he wants to avoid a loop to see how we can help. – kwra Aug 25 '20 at 09:31
  • Because I am not a seasoned programmer, I am very new to it as I need to learn as a side course so Python is the only language I know so I try to avoid loops if I can because we may convert that single string easily to np.datetime64 but when we have a list then it is different. Maybe you could show me a simplified for-loop :) ? Thanks buddy ! – funkymickey Aug 26 '20 at 13:59
  • My first answer is a loop ! It is called list comprehension and it creates a list from the "internal" for loop. But you are right, that will come later if you are new. For the simple for loop: lst = ['2020-01-01', '2020-02-05', '2020-03-07' ] date_list = [] for element in lst: date_list.append(np.datetime64(element)) – kwra Aug 27 '20 at 14:45