Python - get current time in numpy datetime64 format

How to get current date and time using numpy datetime64?

And given the numpy array in which each element is a datetime64 value, how can I get the difference in seconds?

+8
python date time
source share
4 answers

You can use the datetime module to get the current date and pass it to datetime64

import numpy as np import datetime current = np.datetime64(datetime.datetime.now()) 

Now that you have the current time, I would suggest looking at the numpy datetime64 documentation and follow the examples given. Examples on timedelta64 should be especially helpful.

For a specific example, consider the following:

 import numpy as np import datetime current = np.datetime64(datetime.datetime.now()) sample = [np.datetime64('2013-10-22T03:30Z'), np.datetime64('2013-10-22T04:40Z'), np.datetime64('2013-10-22T05:50Z')] diff = [current-t for t in sample] diffSec = [t.item().seconds for t in diff] 

This code causes the diffSec array to contain different values ​​in seconds from the current time to the fetch time

 Out[2]: [1723, 1818, 1913] 

Explaination:

  • The current is first set to the current time using the datetime module.
  • an array of samples is created containing np.datetime64 elements
  • The np.timedelta64 elements are calculated by subtracting each element in the sample from the current time.
  • for each timedelta in diff, the second difference is extracted and stored in a new diffSec array

Obviously, these exact results are not reproducible, as I use the current time to calculate the difference.

+5
source share

You can also do this to get the current date and time:

 import numpy as np np.datetime64('today') # today date np.datetime64('now') # timestamp right now 
+1
source share

Providing Z at the end for the specified time in Zulu format. This is deprecated in numpy python libraries. (Problem on Github: https://github.com/pandas-dev/pandas/issues/12100 ).

 In [1]: import numpy as np In [2]: import datetime 

For the current date, you can use:

 In [3]: current = np.datetime64(datetime.datetime.now()) 

If you are trying to change the time according to your time zone, for example:

 In [3]: previous_date = np.datetime64('2011-01-01T00:00:00-0530') 

OR

 In [3]: previous_date = np.datetime64('2011-01-01T00:00:00Z') 

then you will get a wear message. If you are using a version in which it is already deprecated, you can use the following code

 In [3]: delta = np.timedelta64(5,'h') In [4}: previous_date = np.datetime64('2011-01-01T00:00:00') + delta 
0
source share

Suppose you want to calculate the difference between the array t1 and scalar t0 (they can be either arrays or scalars):

 In [1]: import numpy as np In [2]: t1=np.arange('2001-01-01T00:00', '2001-01-01T00:05', dtype='datetime64') In [3]: t1 Out[3]: array(['2001-01-01T00:00-0200', '2001-01-01T00:01-0200', '2001-01-01T00:02-0200', '2001-01-01T00:03-0200', '2001-01-01T00:04-0200'], dtype='datetime64[m]') In [4]: t0=np.datetime64('2001-01-01T00:00:00') In [5]: t0 Out[5]: numpy.datetime64('2001-01-01T00:00:00-0200') 

The best way to calculate time differences in numpy is to use timedelta64. In the above example, t0 is in minutes and t1 is in seconds. When calculating the time difference, they will both be converted to a smaller unit (in seconds). You just need to subtract them to create the timedelta64 object:

 In [6]: t1-t0 Out[6]: array([ 0, 60, 120, 180, 240], dtype='timedelta64[s]') 

If you want a numerical response, do

 In [7]: (t1-t0).astype('int') Out[7]: array([ 0, 60, 120, 180, 240]) 

Note that I never used a for structure to scan arrays. This will degrade efficiency by preventing vectorization.

-one
source share

All Articles