Skip to content

Commit 25c1942

Browse files
authored
PERF: DatetimeIndex comparison with Timestamp mismatched resos (#52111)
1 parent a21bf9a commit 25c1942

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

pandas/core/arrays/datetimelike.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -957,10 +957,17 @@ def _cmp_method(self, other, op):
957957
if not isinstance(other, type(self)):
958958
# i.e. Timedelta/Timestamp, cast to ndarray and let
959959
# compare_mismatched_resolutions handle broadcasting
960-
other_arr = np.array(other.asm8)
960+
try:
961+
# GH#52080 see if we can losslessly cast to shared unit
962+
other = other.as_unit(self.unit, round_ok=False)
963+
except ValueError:
964+
other_arr = np.array(other.asm8)
965+
return compare_mismatched_resolutions(
966+
self._ndarray, other_arr, op
967+
)
961968
else:
962969
other_arr = other._ndarray
963-
return compare_mismatched_resolutions(self._ndarray, other_arr, op)
970+
return compare_mismatched_resolutions(self._ndarray, other_arr, op)
964971

965972
other_vals = self._unbox(other)
966973
# GH#37462 comparison on i8 values is almost 2x faster than M8/m8

0 commit comments

Comments
 (0)