@@ -45,27 +45,6 @@ def _try_get_item(x):
45
45
except AttributeError :
46
46
return x
47
47
48
- def _indexOp (opname ):
49
- """
50
- Wrapper function for index comparison operations, to avoid
51
- code duplication.
52
- """
53
-
54
- def wrapper (self , other ):
55
- func = getattr (self ._data .view (np .ndarray ), opname )
56
- result = func (np .asarray (other ))
57
-
58
- # technically we could support bool dtyped Index
59
- # for now just return the indexing array directly
60
- if is_bool_dtype (result ):
61
- return result
62
- try :
63
- return Index (result )
64
- except : # pragma: no cover
65
- return result
66
- return wrapper
67
-
68
-
69
48
class InvalidIndexError (Exception ):
70
49
pass
71
50
@@ -1216,13 +1195,6 @@ def __sub__(self, other):
1216
1195
"use .difference()" ,FutureWarning )
1217
1196
return self .difference (other )
1218
1197
1219
- __eq__ = _indexOp ('__eq__' )
1220
- __ne__ = _indexOp ('__ne__' )
1221
- __lt__ = _indexOp ('__lt__' )
1222
- __gt__ = _indexOp ('__gt__' )
1223
- __le__ = _indexOp ('__le__' )
1224
- __ge__ = _indexOp ('__ge__' )
1225
-
1226
1198
def __and__ (self , other ):
1227
1199
return self .intersection (other )
1228
1200
@@ -2380,6 +2352,34 @@ def _evaluate_with_timedelta_like(self, other, op, opstr):
2380
2352
def _evaluate_with_datetime_like (self , other , op , opstr ):
2381
2353
raise TypeError ("can only perform ops with datetime like values" )
2382
2354
2355
+ @classmethod
2356
+ def _add_comparison_methods (cls ):
2357
+ """ add in comparison methods """
2358
+
2359
+ def _make_compare (op ):
2360
+
2361
+ def _evaluate_compare (self , other ):
2362
+ func = getattr (self ._data .view (np .ndarray ), op )
2363
+ result = func (np .asarray (other ))
2364
+
2365
+ # technically we could support bool dtyped Index
2366
+ # for now just return the indexing array directly
2367
+ if is_bool_dtype (result ):
2368
+ return result
2369
+ try :
2370
+ return Index (result )
2371
+ except : # pragma: no cover
2372
+ return result
2373
+
2374
+ return _evaluate_compare
2375
+
2376
+ cls .__eq__ = _make_compare ('__eq__' )
2377
+ cls .__ne__ = _make_compare ('__ne__' )
2378
+ cls .__lt__ = _make_compare ('__lt__' )
2379
+ cls .__gt__ = _make_compare ('__gt__' )
2380
+ cls .__le__ = _make_compare ('__le__' )
2381
+ cls .__ge__ = _make_compare ('__ge__' )
2382
+
2383
2383
@classmethod
2384
2384
def _add_numeric_methods_disabled (cls ):
2385
2385
""" add in numeric methods to disable """
@@ -2530,6 +2530,7 @@ def invalid_op(self, other=None):
2530
2530
2531
2531
Index ._add_numeric_methods_disabled ()
2532
2532
Index ._add_logical_methods ()
2533
+ Index ._add_comparison_methods ()
2533
2534
2534
2535
class CategoricalIndex (Index ):
2535
2536
"""
@@ -2817,8 +2818,42 @@ def convert(c):
2817
2818
cat = Categorical .from_codes (codes , categories = categories )
2818
2819
return CategoricalIndex (cat , name = name )
2819
2820
2821
+ @classmethod
2822
+ def _add_comparison_methods (cls ):
2823
+ """ add in comparison methods """
2824
+
2825
+ def _make_compare (op ):
2826
+
2827
+ def _evaluate_compare (self , other ):
2828
+
2829
+ # we must have only CategoricalIndexes here
2830
+ if not isinstance (other , CategoricalIndex ):
2831
+ raise TypeError ("cannot compare a non-categorical index vs a categorical index" )
2832
+ if not other .categories .equals (self .categories ):
2833
+ raise TypeError ("categorical index comparisions must have the same categories" )
2834
+
2835
+ return getattr (self .codes , op )(other .codes )
2836
+
2837
+ return _evaluate_compare
2838
+
2839
+ def _make_invalid_op (name ):
2840
+
2841
+ def invalid_op (self , other = None ):
2842
+ raise TypeError ("cannot perform {name} with this index type: {typ}" .format (name = name ,
2843
+ typ = type (self )))
2844
+ invalid_op .__name__ = name
2845
+ return invalid_op
2846
+
2847
+ cls .__eq__ = _make_compare ('__eq__' )
2848
+ cls .__ne__ = _make_compare ('__ne__' )
2849
+ cls .__lt__ = _make_invalid_op ('__lt__' )
2850
+ cls .__gt__ = _make_invalid_op ('__gt__' )
2851
+ cls .__le__ = _make_invalid_op ('__le__' )
2852
+ cls .__ge__ = _make_invalid_op ('__ge__' )
2853
+
2820
2854
CategoricalIndex ._add_numeric_methods_disabled ()
2821
2855
CategoricalIndex ._add_logical_methods_disabled ()
2856
+ CategoricalIndex ._add_comparison_methods ()
2822
2857
2823
2858
class NumericIndex (Index ):
2824
2859
"""
0 commit comments