How does one determine the time unit in numpy.datetime64?

1.5k Views Asked by At

I can't see how to reveal the units for numpy.datetime64. Say:

t=np.datetime64(123456789, 'ms' )

What's the method to tell me the units are 'ms'?

2

There are 2 best solutions below

0
On

You can query using:

np.dtype(t)
#dtype('<M8[ms]')

from where you can see the units in ms.

0
On

As of NumPy 1.14 (Jan 2018), the datetime_data function is available to return a tuple of (units, number of base units in step) for a given datetime64 dtype:

>>> dt = np.datetime64(123456789, 'ns')
>>> np.datetime_data(dt)
('ns', 1)
>>> dt_units = np.datetime)data(dt)[0]
>>> print(dt_units)
'ns'