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')
In order to account for the units, I think you need to do something like:
def get_unixtime(dt64):
return dt64.astype('datetime64[s]').astype('int')
Note that this converts to 'seconds' (the [s]
) prior to converting to integers. This works on NumPy 1.12.1.
numpy datetime64 has variable units:
Extracted from official doc:
The unit for internal storage is automatically selected from the form of the string, and can be either a date unit or a time unit. The date units are years (‘Y’), months (‘M’), weeks (‘W’), and days (‘D’), while the time units are hours (‘h’), minutes (‘m’), seconds (‘s’), milliseconds (‘ms’), and some additional SI-prefix seconds-based units.
So, first we need to check the current unit using dtype, for example:
>>> now = np.datetime64(datetime.datetime.now())
>>> now.dtype
# for ns unit, use:
dtype('<M8[ns]')
now.astype('int64')/1e9, dtype='int32'
# for us unit, use:
dtype('<M8[us]')
now.astype('int64')/1e6, dtype='int32'
# for ms unit, use:
dtype('<M8[ms]')
now.astype('int64')/1e3, dtype='int32'
and so on....
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
I get inconsistent results for the value of np.datetime64('now')
on numpy 1.6.1 vs. 1.7.
This works on both:
>>> import datetime
>>> import numpy as np
>>> now = np.datetime64(datetime.datetime.now())
>>> (now.astype('uint64') / 1e6).astype('uint32')
1344447810
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.
def get_unixtime(time):
return (time.astype(np.int64)/1e6).astype(np.int64)
get_unixtime(np.datetime64('now'))
Does seem to return the UNIX timestamp, I have only checked with a few dates.