Skip to content

Commit 1dfe01b

Browse files
Backport PR #52111 on branch 2.0.x (PERF: DatetimeIndex comparison with Timestamp mismatched resos) (#52160)
Backport PR #52111: PERF: DatetimeIndex comparison with Timestamp mismatched resos Co-authored-by: jbrockmendel <[email protected]>
1 parent 7d5d123 commit 1dfe01b

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
@@ -959,10 +959,17 @@ def _cmp_method(self, other, op):
959959
if not isinstance(other, type(self)):
960960
# i.e. Timedelta/Timestamp, cast to ndarray and let
961961
# compare_mismatched_resolutions handle broadcasting
962-
other_arr = np.array(other.asm8)
962+
try:
963+
# GH#52080 see if we can losslessly cast to shared unit
964+
other = other.as_unit(self.unit, round_ok=False)
965+
except ValueError:
966+
other_arr = np.array(other.asm8)
967+
return compare_mismatched_resolutions(
968+
self._ndarray, other_arr, op
969+
)
963970
else:
964971
other_arr = other._ndarray
965-
return compare_mismatched_resolutions(self._ndarray, other_arr, op)
972+
return compare_mismatched_resolutions(self._ndarray, other_arr, op)
966973

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

0 commit comments

Comments
 (0)