4

What is the proper method to compare the date portion of two numpy.datetime64's?

A:  2011-01-10  Type:  <type 'numpy.datetime64'>
B:  2011-01-10T09:00:00.000000-0700  Type:  <type 'numpy.datetime64'>

The above example would return false by comparing (A == B)

CamC
  • 89
  • 1
  • 6

1 Answers1

3

You'll want to strip your datetime64 of time information before comparison by specifying the 'datetime64[D]' data type, like this:

>>> a = numpy.datetime64('2011-01-10')
>>> b = numpy.datetime64('2011-01-10T09:00:00.000000-0700')
>>> a == b
False
>>> a.astype('datetime64[D]') == b.astype('datetime64[D]')
True

I couldn't get numpy to create an array of datetime64[D] values from the string you gave for b above, by the way. I got this error:

>>> b = numpy.array(['2011-01-10T09:00:00.000000-0700'], dtype='datetime64[D]')
TypeError: Cannot parse "2011-01-10T09:00:00.000000-0700" as unit 'D' using casting rule 'same_kind'
pneumatics
  • 2,836
  • 1
  • 27
  • 27