Skip to content

Commit 85d982d

Browse files
committed
ENH: tweak implementation of Timestamp.__richcmp__ to be faster
1 parent c252129 commit 85d982d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pandas/src/datetime.pyx

+14-6
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ cdef class _Timestamp(datetime):
389389

390390
if isinstance(other, _Timestamp):
391391
ots = other
392-
elif isinstance(other, datetime):
392+
elif type(other) is datetime:
393+
if self.nanosecond == 0:
394+
val = self.to_datetime()
395+
return PyObject_RichCompareBool(val, other, op)
396+
393397
try:
394398
ots = Timestamp(other)
395399
except ValueError:
@@ -458,10 +462,14 @@ cdef class _Timestamp(datetime):
458462
raise Exception('Cannot compare tz-naive and tz-aware timestamps')
459463

460464
cpdef to_datetime(self):
461-
return datetime(self.year, self.month, self.day,
462-
self.hour, self.minute, self.second,
463-
self.microsecond, tzinfo=self.tzinfo)
464-
465+
cdef:
466+
pandas_datetimestruct dts
467+
_TSObject ts
468+
ts = convert_to_tsobject(self, self.tzinfo)
469+
dts = ts.dts
470+
return datetime(dts.year, dts.month, dts.day,
471+
dts.hour, dts.min, dts.sec,
472+
dts.us, ts.tzinfo)
465473

466474
def __add__(self, other):
467475
if is_integer_object(other):
@@ -786,7 +794,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
786794
iresult = result.view('i8')
787795
for i in range(n):
788796
val = values[i]
789-
if util._checknull(val):
797+
if util._checknull(val) or val is NaT:
790798
iresult[i] = iNaT
791799
elif PyDateTime_Check(val):
792800
if val.tzinfo is not None:

pandas/src/util.pxd

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from numpy cimport ndarray
22
cimport numpy as cnp
3+
cimport cpython
34

45
cdef extern from "numpy_helper.h":
56
inline void set_array_owndata(ndarray ao)
@@ -61,7 +62,7 @@ cdef inline is_array(object o):
6162

6263
cdef inline bint _checknull(object val):
6364
try:
64-
return bool(val is None or val != val)
65+
return val is None or (cpython.PyFloat_Check(val) and val != val)
6566
except ValueError:
6667
return False
6768

0 commit comments

Comments
 (0)