Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numpy datetime64 to long integer and back

How to convert NumPy datetime64 to a long ineteger and back?

import numpy as np
import datetime

np.datetime64(datetime.datetime.now()).astype(long)

Gives a value of 1511975032478959

np.datetime64(np.datetime64(datetime.datetime.now()).astype(long))

Gives an error:

ValueError: Converting an integer to a NumPy datetime requires a specified unit
like image 785
Inquisitor Avatar asked Jun 17 '26 19:06

Inquisitor


2 Answers

You need to specify the units of the long int (in this case, microseconds).

 np.datetime64(np.datetime64(datetime.datetime.now()).astype(long), 'us')

returns

 numpy.datetime64('2017-11-29T17:11:44.638713')
like image 191
Tim Avatar answered Jun 20 '26 09:06

Tim


'us' converts to microseconds. If you require a different format use: enter image description here

as shown here

like image 25
horseshoe Avatar answered Jun 20 '26 08:06

horseshoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!