Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot numpy arrays convert from datetime to np.datetime64 implicitly?

Say, I have a datetime:

given_time = datetime(2013, 10, 8, 0, 0, 33, 945109,
                      tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=60, 
                                                             name=None))

I would like to transform it into np.datetime64:

np.datetime64(given_time)
> numpy.datetime64('2013-10-08T00:00:33.945109+0100')

It works well. However, if I have an array of given_time:

given_times = np.array([given_time]*3) # dtype is object

Both given_times.astype('datetime64') and given_times = np.array([given_time] * 3, dtype=np.datetime64) would trigger TypeError: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'

So, I have to specify the unit:

given_times.astype('datetime64[us]')
# or
given_times = np.array([given_time]*3, dtype='datetime64[us]')

My question is, why do I have to specify the unit here? It doesn't require unit in np.datatime64 constructor.

like image 898
colinfang Avatar asked Feb 24 '14 13:02

colinfang


People also ask

What is Numpy datetime64?

datetime64() method, we can get the date in a numpy array in a particular format i.e year-month-day by using numpy. datetime64() method. Syntax : numpy.datetime64(date) Return : Return the date in a format 'yyyy-mm-dd'.

What is Timedelta64?

Timedelta64 computations between two UTC dates can be wrong by an integer number of SI seconds. Example. Compute the number of SI seconds between “2021-01-01 12:56:23.423 UTC” and “2001-01-01 00:00:00.000 UTC”: >>> ( ... np.

How do you get all the dates corresponding to the month of July 2016 Numpy?

In NumPy to display all the dates for a particular month, we can do it with the help of NumPy. arrange() pass the first parameter the particular month and the second parameter the next month and the third parameter is the datatype datetime64[D]. It will return all the dates for the particular month.


1 Answers

I know it's an old question, but I'd try to answer in case anyone else comes across this.

  1. As of 1.11, numpy doesn't try to automatically convert iterables of date/datetime objects to datetime64 arrays, this is pretty clear from this excerpt in the test suite:
# at the moment, we don't automatically convert these to datetime64

dt = datetime.date(1970, 1, 1)
arr = np.array([dt])
assert_equal(arr.dtype, np.dtype('O'))

dt = datetime.datetime(1970, 1, 1, 12, 30, 40)     
arr = np.array([dt])
assert_equal(arr.dtype, np.dtype('O'))

Ideally, numpy would figure that datetime64 with correct units could be used; see this issue.

  1. When constructing datetime64 from a scalar, the unit it set to M8[D] for date objects and to M8[us] for datetime objects (a relevant test).

  2. When you specify dtype='datetime64', or, similarly, dtype='M8', the units are set to "generic", which later resolves to M8[D] (although it would be logical to have it resolve to M8[D], see this issue):

>>> np.datetime_data(np.dtype('datetime64'))
('generic', 1)
>>> np.datetime_data(np.dtype('M8'))
('generic', 1)
>>> np.datetime_data(np.dtype('M8[D]'))
('D', 1)
>>> np.datetime_data(np.dtype('M8[us]'))
('us', 1)
  1. given_times.astype('datetime64') no longer raises an exception -- this was fixed in 1.11.

  2. Starting from 1.11, datetime64 objects are timezone-naive, so passing a datetime object with tzinfo set like in the provided example will trigger a deprecation warning.

like image 180
aldanor Avatar answered Oct 13 '22 22:10

aldanor