How do I get the UNIX time from a numpy.datetime64 or numpy.datetime_?
As in for example:
np.datetime_('2012-08-08 13:37:00')
How do I get the UNIX time from a numpy.datetime64 or numpy.datetime_?
As in for example:
np.datetime_('2012-08-08 13:37:00')
I wanted to post the solution I had found that I think might be a bit better than doing a conversion to uint as I feel there might be issues in that conversion of types.
>>> import numpy as np
>>> now = np.datetime64('now')
>>> ux_time = now.astype(np.timedelta64) / np.timedelta64(1, 'ms')
I feel this solution is a bit better since it allows you to choose your unix time units. For the project I'm working on we use 'ms' but you could specify a different unit if needed.
Additionally this allows intake of an array of datetime64 to convert to timedelta64 using numpy:
>>> date_time_array.astype(np.timedelta64) / np.timedelta64(1, 'ms')
I use this to translate np.datetime64 columns taken from pandas into unixtime arrays
First you have to know the storage units of the array. Then you view the array as a 64 bit integer and divide by the appropriate scaling factor to get back to seconds. For example if your datetime array is stored with storage units of microseconds (dtype=<M8[us]) you would do this:
unix_time = dtarray.view("i8") / 1e6
Here's an extended and modified version of @farenorth 's answer, which allows to specify the precision of the output:
from datetime import datetime, timezone
import numpy as np
# np.__version__: '1.21.5'
def get_unixtime(dt64, unit='s'):
return dt64.astype(f'datetime64[{unit}]').astype(np.int64)
print(datetime(2022,3,2,tzinfo=timezone.utc).timestamp())
# 1646179200.0 # unix time in seconds
dt = np.datetime64(datetime(2022,3,2)) # tz naive in numpy!
for unit in 's', 'ms', 'us', 'ns':
print(f"precision: {unit}, -> {get_unixtime(dt, unit)}")
# precision: s, -> 1646179200
# precision: ms, -> 1646179200000
# precision: us, -> 1646179200000000
# precision: ns, -> 1646179200000000000
As a side-note, we cannot use int or 'int' (native Python type) here as that gives incorrect results. Related: Error when converting numpy.datetime64 to int.