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