Skip to content

Categorical Arithmetic/Comparison Rules #19513

Closed
@jbrockmendel

Description

@jbrockmendel

Series[not-categorical] > CategoricalIndex is inconsistent with the reversed operation. Which one is canonical?

ser = pd.Series([1, 2, 3])
idx = pd.CategoricalIndex(['A', 'B', 'A'])

>>> ser > idx
0    False
1    False
2    False

>>> idx < ser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/indexes/category.py", line 752, in _evaluate_compare
    return getattr(self.values, opname)(other)
  File "pandas/core/arrays/categorical.py", line 56, in f
    raise TypeError("Unordered Categoricals can only compare "
TypeError: Unordered Categoricals can only compare equality or not

>>> ser > idx.values
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/ops.py", line 819, in wrapper
    .format(op=op, typ=self.dtype))
TypeError: Cannot compare a Categorical for op <built-in function gt> with Series of dtype int64.
If you want to compare values, use 'series <op> np.asarray(other)'.

I'm guessing the right thing to do is to a) have Series[categorical].__op__ wrap CategoricalIndex.__op__, and b) have Series[non-categorical] to dispatch to reversed-op for is_categorical_dtype(other), want to confirm before making a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions