def _cat_compare_op(op):

    def f(self, other):
        if isinstance(other, ABCSeries):
            return NotImplemented
        if (not self.ordered):
            if (op in ['__lt__', '__gt__', '__le__', '__ge__']):
                raise TypeError('Unordered Categoricals can only compare equality or not')
        if isinstance(other, Categorical):
            msg = "Categoricals can only be compared if 'categories' are the same."
            if (len(self.categories) != len(other.categories)):
                raise TypeError((msg + ' Categories are different lengths'))
            elif (self.ordered and (not (self.categories == other.categories).all())):
                raise TypeError(msg)
            elif (not (set(self.categories) == set(other.categories))):
                raise TypeError(msg)
            if (not (self.ordered == other.ordered)):
                raise TypeError("Categoricals can only be compared if 'ordered' is the same")
            if ((not self.ordered) and (not self.categories.equals(other.categories))):
                other_codes = _get_codes_for_values(other, self.categories)
            else:
                other_codes = other._codes
            na_mask = ((self._codes == (- 1)) | (other_codes == (- 1)))
            f = getattr(self._codes, op)
            ret = f(other_codes)
            if na_mask.any():
                ret[na_mask] = False
            return ret
        other = lib.item_from_zerodim(other)
        if is_scalar(other):
            if (other in self.categories):
                i = self.categories.get_loc(other)
                return getattr(self._codes, op)(i)
            elif (op == '__eq__'):
                return np.repeat(False, len(self))
            elif (op == '__ne__'):
                return np.repeat(True, len(self))
            else:
                msg = 'Cannot compare a Categorical for op {op} with a scalar, which is not a category.'
                raise TypeError(msg.format(op=op))
        else:
            if (op in ['__eq__', '__ne__']):
                return getattr(np.array(self), op)(np.array(other))
            msg = "Cannot compare a Categorical for op {op} with type {typ}.\nIf you want to compare values, use 'np.asarray(cat) <op> other'."
            raise TypeError(msg.format(op=op, typ=type(other)))
    f.__name__ = op
    return f
def _cat_compare_op(op):

    def f(self, other):
        if isinstance(other, ABCSeries):
            return NotImplemented
        if (not self.ordered):
            if (op in ['__lt__', '__gt__', '__le__', '__ge__']):
                raise TypeError('Unordered Categoricals can only compare equality or not')
        if isinstance(other, Categorical):
            msg = "Categoricals can only be compared if 'categories' are the same."
            if (len(self.categories) != len(other.categories)):
                raise TypeError((msg + ' Categories are different lengths'))
            elif (self.ordered and (not (self.categories == other.categories).all())):
                raise TypeError(msg)
            elif (not (set(self.categories) == set(other.categories))):
                raise TypeError(msg)
            if (not (self.ordered == other.ordered)):
                raise TypeError("Categoricals can only be compared if 'ordered' is the same")
            if ((not self.ordered) and (not self.categories.equals(other.categories))):
                other_codes = _get_codes_for_values(other, self.categories)
            else:
                other_codes = other._codes
            na_mask = ((self._codes == (- 1)) | (other_codes == (- 1)))
            f = getattr(self._codes, op)
            ret = f(other_codes)
            if na_mask.any():
                ret[na_mask] = False
            return ret
        other = lib.item_from_zerodim(other)
        if is_scalar(other):
            if (other in self.categories):
                i = self.categories.get_loc(other)
                return getattr(self._codes, op)(i)
            elif (op == '__eq__'):
                return np.repeat(False, len(self))
            elif (op == '__ne__'):
                return np.repeat(True, len(self))
            else:
                msg = 'Cannot compare a Categorical for op {op} with a scalar, which is not a category.'
                raise TypeError(msg.format(op=op))
        else:
            if (op in ['__eq__', '__ne__']):
                return getattr(np.array(self), op)(np.array(other))
            msg = "Cannot compare a Categorical for op {op} with type {typ}.\nIf you want to compare values, use 'np.asarray(cat) <op> other'."
            raise TypeError(msg.format(op=op, typ=type(other)))
    f.__name__ = op
    return f