AAA
AAA

Reputation: 735

compare a np array of datetime with a single datetime in python

I am trying to compare an array of datetime to a single datetime

tt = np.array([['1993-09-01T00:00:00.000000000', 
        '2005-12-01T00:00:00.000000000'],
       ['1992-07-01T00:00:00.000000000', 
        '2009-05-01T00:00:00.000000000']], dtype='datetime64[ns]')
tt > dt.datetime(2001,1,1)

It gives me an error. TypeError: '>' not supported between instances of 'int' and 'datetime.datetime' May I know how to compare my date time array with a single datetime?

The result i expect it np.arrary([[False, True],[False, True]])

Upvotes: 1

Views: 2016

Answers (2)

Daniel Perez
Daniel Perez

Reputation: 6903

When trying to compare a numpy object with something of another type, numpy will try to make the conversion. Here, the target is a Python object which is not part of numpy, so numpy will cast the numpy date into a Python object. One would expect the resulting Python object to be a datetime.datetime but as it does not support nanoseconds, numpy converts it to an int instead.

See the relevant part of the numpy code. https://github.com/numpy/numpy/blob/608329acac78b98e9c1044dca23a4f287d639b24/numpy/core/src/multiarray/datetime.c#L2847

You can for example try:

>>> np.array(['2009-05-01T00:00:00.000000000'], dtype='datetime64[ns]').tolist()
[1241136000000000000]

Python int cannot be compared with datetime.datetime, hence the error.

However, if the numpy datetime can be expressed as a datetime.datetime (if its precision is above microseconds and it is in the range of valid datetimes), it will be converted in a datetime.datetime instead of an int. Try for example:

>>> np.array(['2009-05-01T00:00:00.000000000'], dtype='datetime64[us]').tolist()
[datetime.datetime(2009, 5, 1, 0, 0)]

So for your example to work, you could simply change the dtype from datetime64[ns] to datetime64[us] and it should be behave as you expected.

Note that this behavior is not related to arrays in particular.

>>> np.datetime64('2009-05-01T00:00:00.000000000', 'ns') > dt.datetime(2001, 1, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'datetime.datetime'
>>> np.datetime64('2009-05-01T00:00:00.000000000', 'us') > dt.datetime(2001, 1, 1)
True

Upvotes: 0

astiegler
astiegler

Reputation: 315

The reason is that datetime object are different from numpy.datetime object. So you're comparing different objects hence the error. Try this line of code instead:

tt = np.array([['1993-09-01T00:00:00.000000000', 
        '2005-12-01T00:00:00.000000000'],
       ['1992-07-01T00:00:00.000000000', 
        '2009-05-01T00:00:00.000000000']], dtype='datetime64')
tt > np.datetime64('2001-01-01')

You will get the desired result!

Upvotes: 1

Related Questions