Skip to content

Commit cb92366

Browse files
committed
De-duplicate redundant comparison method code
1 parent c8ce3d0 commit cb92366

File tree

3 files changed

+4
-65
lines changed

3 files changed

+4
-65
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from pandas.util._decorators import deprecate_kwarg
4242

4343

44-
def _make_comparison_op(op, cls):
44+
def _make_comparison_op(cls, op):
4545
# TODO: share code with indexes.base version? Main difference is that
4646
# the block for MultiIndex was removed here.
4747
def cmp_method(self, other):
@@ -740,6 +740,9 @@ def __isub__(self, other):
740740
# --------------------------------------------------------------
741741
# Comparison Methods
742742

743+
# Called by _add_comparison_methods defined in ExtensionOpsMixin
744+
_create_comparison_method = classmethod(_make_comparison_op)
745+
743746
def _evaluate_compare(self, other, op):
744747
"""
745748
We have been called because a comparison between
@@ -773,19 +776,6 @@ def _evaluate_compare(self, other, op):
773776
result[mask] = filler
774777
return result
775778

776-
# TODO: get this from ExtensionOpsMixin
777-
@classmethod
778-
def _add_comparison_methods(cls):
779-
""" add in comparison methods """
780-
# DatetimeArray and TimedeltaArray comparison methods will
781-
# call these as their super(...) methods
782-
cls.__eq__ = _make_comparison_op(operator.eq, cls)
783-
cls.__ne__ = _make_comparison_op(operator.ne, cls)
784-
cls.__lt__ = _make_comparison_op(operator.lt, cls)
785-
cls.__gt__ = _make_comparison_op(operator.gt, cls)
786-
cls.__le__ = _make_comparison_op(operator.le, cls)
787-
cls.__ge__ = _make_comparison_op(operator.ge, cls)
788-
789779

790780
DatetimeLikeArrayMixin._add_comparison_methods()
791781

pandas/core/indexes/datetimes.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,6 @@ def func(self, *args, **kwargs):
8282
return func
8383

8484

85-
def _dt_index_cmp(cls, op):
86-
"""
87-
Wrap comparison operations to convert datetime-like to datetime64
88-
"""
89-
opname = '__{name}__'.format(name=op.__name__)
90-
91-
def wrapper(self, other):
92-
result = getattr(DatetimeArrayMixin, opname)(self, other)
93-
if is_bool_dtype(result):
94-
return result
95-
return Index(result)
96-
97-
return compat.set_function_name(wrapper, opname, cls)
98-
99-
10085
def _new_DatetimeIndex(cls, d):
10186
""" This is called upon unpickling, rather than the default which doesn't
10287
have arguments and breaks __new__ """
@@ -233,16 +218,6 @@ def _join_i8_wrapper(joinf, **kwargs):
233218
_left_indexer_unique = _join_i8_wrapper(
234219
libjoin.left_join_indexer_unique_int64, with_indexers=False)
235220

236-
@classmethod
237-
def _add_comparison_methods(cls):
238-
""" add in comparison methods """
239-
cls.__eq__ = _dt_index_cmp(cls, operator.eq)
240-
cls.__ne__ = _dt_index_cmp(cls, operator.ne)
241-
cls.__lt__ = _dt_index_cmp(cls, operator.lt)
242-
cls.__gt__ = _dt_index_cmp(cls, operator.gt)
243-
cls.__le__ = _dt_index_cmp(cls, operator.le)
244-
cls.__ge__ = _dt_index_cmp(cls, operator.ge)
245-
246221
_engine_type = libindex.DatetimeEngine
247222

248223
tz = None

pandas/core/indexes/timedeltas.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ def f(self):
5050
return property(f)
5151

5252

53-
def _td_index_cmp(cls, op):
54-
"""
55-
Wrap comparison operations to convert timedelta-like to timedelta64
56-
"""
57-
opname = '__{name}__'.format(name=op.__name__)
58-
59-
def wrapper(self, other):
60-
result = getattr(TimedeltaArrayMixin, opname)(self, other)
61-
if is_bool_dtype(result):
62-
# support of bool dtype indexers
63-
return result
64-
return Index(result)
65-
66-
return compat.set_function_name(wrapper, opname, cls)
67-
68-
6953
class TimedeltaIndex(TimedeltaArrayMixin, DatetimeIndexOpsMixin,
7054
TimelikeOps, Int64Index):
7155
"""
@@ -153,16 +137,6 @@ def _join_i8_wrapper(joinf, **kwargs):
153137
_datetimelike_methods = ["to_pytimedelta", "total_seconds",
154138
"round", "floor", "ceil"]
155139

156-
@classmethod
157-
def _add_comparison_methods(cls):
158-
""" add in comparison methods """
159-
cls.__eq__ = _td_index_cmp(cls, operator.eq)
160-
cls.__ne__ = _td_index_cmp(cls, operator.ne)
161-
cls.__lt__ = _td_index_cmp(cls, operator.lt)
162-
cls.__gt__ = _td_index_cmp(cls, operator.gt)
163-
cls.__le__ = _td_index_cmp(cls, operator.le)
164-
cls.__ge__ = _td_index_cmp(cls, operator.ge)
165-
166140
_engine_type = libindex.TimedeltaEngine
167141

168142
_comparables = ['name', 'freq']

0 commit comments

Comments
 (0)