Reputation: 71
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
Upvotes: 7
Views: 19793
Reputation: 1451
This worked for me in Python3 (other answers seem to focus in Python2):
now = datetime.datetime.now().timestamp()
np.datetime64(int(now),'s')
2022-07-01T14:25:22
Upvotes: 0
Reputation: 1477
'us' converts to microseconds. If you require a different format use:
as shown here
Upvotes: 4
Reputation: 2843
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')
Upvotes: 7