Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement arith ops on pd.Categorical #21213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=E1101,W0232
import operator

import numpy as np
from warnings import warn
Expand Down Expand Up @@ -60,6 +61,21 @@
Use 'allow_fill=False' to accept the new behavior.""")


def _cat_arithmetic_op(op, reverse=False):
# arithmetic operations are disabled
def func(self, other):
from pandas.core.ops import _get_opstr
str_rep = _get_opstr(op, self.__class__)
raise TypeError("{typ} cannot perform the operation "
"{op}".format(typ=type(self).__name__, op=str_rep))

opname = op.__name__
if reverse:
opname = '__r' + opname[2:]
func.__name__ = opname
return func


def _cat_compare_op(op):
def f(self, other):
# On python2, you can usually compare any type to any type, and
Expand Down Expand Up @@ -1203,6 +1219,24 @@ def map(self, mapper):
__le__ = _cat_compare_op('__le__')
__ge__ = _cat_compare_op('__ge__')

__add__ = _cat_arithmetic_op(operator.add)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so my ops PR does almost exactly this (for extension arrays). I think we should maybe put it in a mixin? so can capture ops definitions generically (but of course can do that later).

Copy link
Member Author

@jbrockmendel jbrockmendel May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. For the most part I'm happy being hands-off with Categorical since others are more familiar with it. I figured this would be an easy way to demonstrate what I have in mind for discussions in #21160, #20889.

__radd__ = _cat_arithmetic_op(operator.add, True)
__sub__ = _cat_arithmetic_op(operator.sub)
__rsub__ = _cat_arithmetic_op(operator.sub, True)
__mul__ = _cat_arithmetic_op(operator.mul)
__rmul__ = _cat_arithmetic_op(operator.mul, True)
__truediv__ = _cat_arithmetic_op(operator.truediv)
__rtruediv__ = _cat_arithmetic_op(operator.truediv, True)
__floordiv__ = _cat_arithmetic_op(operator.floordiv)
__rfloordiv__ = _cat_arithmetic_op(operator.floordiv, True)
__mod__ = _cat_arithmetic_op(operator.mod)
__rmod__ = _cat_arithmetic_op(operator.mod, True)
__pow__ = _cat_arithmetic_op(operator.pow)
__rpow__ = _cat_arithmetic_op(operator.pow, True)
if compat.PY2:
__div__ = __truediv__
__rdiv__ = __rtruediv__

# for Series/ndarray like compat
@property
def shape(self):
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,11 @@ def wrapper(left, right):
dtype=result.dtype)

elif is_categorical_dtype(left):
raise TypeError("{typ} cannot perform the operation "
"{op}".format(typ=type(left).__name__, op=str_rep))
# raises TypeError
result = dispatch_to_index_op(op, left, right, pd.Categorical)
return construct_result(left, result,
index=left.index, name=res_name,
dtype=result.dtype)

lvalues = left.values
rvalues = right
Expand Down