I have a numpy array x
of dtype np.int64
representing nanoseconds. I'm able to convert this into a numpy array of dtype np.datetime64
with the following code:
np.array([np.datetime64(int(a), 'ns') for a in x])
Is there a better way to do this, that avoids python list comprehension?
You can do:
x = np.array([1e9, 5e9, 1e10], dtype=np.int64)
x.astype('datetime64[ns]')
Output:
array(['1970-01-01T00:00:01.000000000', '1970-01-01T00:00:05.000000000',
'1970-01-01T00:00:10.000000000'], dtype='datetime64[ns]')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With