|
58 | 58 |
|
59 | 59 | def _cat_compare_op(op):
|
60 | 60 | opname = f"__{op.__name__}__"
|
| 61 | + fill_value = True if op is operator.ne else False |
61 | 62 |
|
62 | 63 | @unpack_zerodim_and_defer(opname)
|
63 | 64 | def func(self, other):
|
@@ -92,26 +93,23 @@ def func(self, other):
|
92 | 93 | else:
|
93 | 94 | other_codes = other._codes
|
94 | 95 |
|
95 |
| - f = getattr(self._codes, opname) |
96 |
| - ret = f(other_codes) |
| 96 | + ret = op(self._codes, other_codes) |
97 | 97 | mask = (self._codes == -1) | (other_codes == -1)
|
98 | 98 | if mask.any():
|
99 |
| - # In other series, the leads to False, so do that here too |
100 |
| - if opname == "__ne__": |
101 |
| - ret[(self._codes == -1) & (other_codes == -1)] = True |
102 |
| - else: |
103 |
| - ret[mask] = False |
| 99 | + ret[mask] = fill_value |
104 | 100 | return ret
|
105 | 101 |
|
106 | 102 | if is_scalar(other):
|
107 | 103 | if other in self.categories:
|
108 | 104 | i = self.categories.get_loc(other)
|
109 |
| - ret = getattr(self._codes, opname)(i) |
| 105 | + ret = op(self._codes, i) |
110 | 106 |
|
111 | 107 | if opname not in {"__eq__", "__ge__", "__gt__"}:
|
112 |
| - # check for NaN needed if we are not equal or larger |
| 108 | + # GH#29820 performance trick; get_loc will always give i>=0, |
| 109 | + # so in the cases (__ne__, __le__, __lt__) the setting |
| 110 | + # here is a no-op, so can be skipped. |
113 | 111 | mask = self._codes == -1
|
114 |
| - ret[mask] = False |
| 112 | + ret[mask] = fill_value |
115 | 113 | return ret
|
116 | 114 | else:
|
117 | 115 | return ops.invalid_comparison(self, other, op)
|
|
0 commit comments