How do I get the current date and time using numpy datetime64?
And given a numpy array in which each element is a datetime64 value, how do I get the difference in seconds?
You could also do this to get the current date and time:
import numpy as np
np.datetime64('today') # today's date
np.datetime64('now') # timestamp right now
np.datetime64('now') == np.datetime64(datetime.datetime.now()) is False so depending what precision you want, you might want to chose one over the other.np.datetime64(datetime.datetime.now()) is more precise (microseconds vs. seconds).You could 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 datetime I would suggest looking over the numpy datetime64 documentation and following the examples provided. The examples on timedelta64 should be particularly helpful.
For a concrete 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 results in the diffSec array containing the different in seconds from the current time to the sample times
Out[2]: [1723, 1818, 1913]
Explaination:
Obviously these exact results aren't reproducible as I am using the current time to calculate the difference.
Providing Z in the end for the time specified the time is in Zulu format. This is being deprecated in the python numpy libraries. (Issue 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 try to alter time according to your timezone, 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 would get a DeprecationWarning. If you are in a time using a version where 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
Suppose that you want to calculate the difference between the array t1 and the scalar t0 (they could be both 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 compute time differences in numpy is to use timedelta64. In the example above, t0 is in minutes and t1 is in seconds. When calculating the time difference,
they will both be converted to the smaller unity (seconds). You just need to subtract them
to create a timedelta64 object:
In [6]: t1-t0
Out[6]: array([ 0, 60, 120, 180, 240], dtype='timedelta64[s]')
If you wish the response in a numeric format, do
In [7]: (t1-t0).astype('int')
Out[7]: array([ 0, 60, 120, 180, 240])
Please notice that I never used the for structure to scan the arrays. It would impair the efficiency by preventing vectorization.