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?
4 Answers 4
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
-
1Note that
np.datetime64('now') == np.datetime64(datetime.datetime.now())
isFalse
so depending what precision you want, you might want to chose one over the other.Sardathrion - against SE abuse– Sardathrion - against SE abuse2017年07月18日 13:41:50 +00:00Commented Jul 18, 2017 at 13:41 -
1Which is more precise @Sardathrion?jebob– jebob2018年11月22日 17:36:32 +00:00Commented Nov 22, 2018 at 17:36
-
np.datetime64(datetime.datetime.now())
is more precise (microseconds vs. seconds).gosuto– gosuto2020年02月09日 14:48:50 +00:00Commented Feb 9, 2020 at 14:48
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:
- current is first set to the current time using the datetime module.
- a sample array is created which contain np.datetime64 elements
- the np.timedelta64 elements are calculated by subtracting each element in sample from the current time.
- for each timedelta in diff the second difference is extracted and saved in a new array diffSec
Obviously these exact results aren't reproducible as I am using the current time to calculate the difference.
-
int(np.datetime64(datetime.now(timezone.utc))) <ipython-input-6-108573c572ed>:1: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future int(np.datetime64(datetime.now(timezone.utc)))user3673– user36732021年12月10日 19:01:36 +00:00Commented Dec 10, 2021 at 19:01
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.