20

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?

asked Oct 19, 2013 at 1:18

4 Answers 4

23

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 
answered Jun 17, 2017 at 7:51
3
  • 1
    Note that 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. Commented Jul 18, 2017 at 13:41
  • 1
    Which is more precise @Sardathrion? Commented Nov 22, 2018 at 17:36
  • np.datetime64(datetime.datetime.now()) is more precise (microseconds vs. seconds). Commented Feb 9, 2020 at 14:48
9

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:

  1. current is first set to the current time using the datetime module.
  2. a sample array is created which contain np.datetime64 elements
  3. the np.timedelta64 elements are calculated by subtracting each element in sample from the current time.
  4. 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.

answered Oct 19, 2013 at 2:39
1
  • 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))) Commented Dec 10, 2021 at 19:01
2

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
answered Dec 22, 2016 at 7:24
1

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.

answered Dec 28, 2014 at 9:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.