Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numpy array of strings to datetime

I have an array of strings, for example

import numpy as np
foo = np.array( [b'2014-04-05', b'2014-04-06', b'2014-04-07'] )

To check for the data type of the array, I print it with

print( foo.dtype )

which results in |S10. Obviously, it consists of strings of length 10. I want to convert it into NumPy's datetime64 type.

More precisely, I want to change the data type of the array without looping through a for-loop and copying it element-wise into a new array (the real array is actually very large). Naive as I am, I thought the following might work:

[ np.datetime64(x) for x in foo ]

Spoiler: it does not. Printing the data type of the array results in the same output as before (i.e., |S10).

Is there a memory efficient way to convert the data type of the existing array without the necessity of copying everything to a new array?

like image 957
Alf Avatar asked Oct 04 '18 20:10

Alf


1 Answers

Use .astype, with copy=False to avoid creating a copy:

foo = np.array( [b'2014-04-05', b'2014-04-06', b'2014-04-07'] )

foo = foo.astype('datetime64',copy=False)

>>> foo
array(['2014-04-05', '2014-04-06', '2014-04-07'], dtype='datetime64[D]')
like image 184
sacuL Avatar answered Nov 20 '22 18:11

sacuL