Skip to content

Commit

Permalink
remove core.computation.ops.Div resolves pandas-dev#21374 pandas-dev#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Mutricy committed Jun 28, 2024
1 parent a89f208 commit ee4a2b6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 55 deletions.
5 changes: 1 addition & 4 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
UNARY_OPS_SYMS,
BinOp,
Constant,
Div,
FuncNode,
Op,
Term,
Expand Down Expand Up @@ -374,6 +373,7 @@ class BaseExprVisitor(ast.NodeVisitor):
"Add",
"Sub",
"Mult",
"Div",
None,
"Pow",
"FloorDiv",
Expand Down Expand Up @@ -537,9 +537,6 @@ def visit_BinOp(self, node, **kwargs):
left, right = self._maybe_downcast_constants(left, right)
return self._maybe_evaluate_binop(op, op_class, left, right)

def visit_Div(self, node, **kwargs):
return lambda lhs, rhs: Div(lhs, rhs)

def visit_UnaryOp(self, node, **kwargs):
op = self.visit(node.op)
operand = self.visit(node.operand)
Expand Down
51 changes: 0 additions & 51 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,31 +328,6 @@ def _not_in(x, y):
_binary_ops_dict.update(d)


def _cast_inplace(terms, acceptable_dtypes, dtype) -> None:
"""
Cast an expression inplace.
Parameters
----------
terms : Op
The expression that should cast.
acceptable_dtypes : list of acceptable numpy.dtype
Will not cast if term's dtype in this list.
dtype : str or numpy.dtype
The dtype to cast to.
"""
dt = np.dtype(dtype)
for term in terms:
if term.type in acceptable_dtypes:
continue

try:
new_value = term.value.astype(dt)
except AttributeError:
new_value = dt.type(term.value)
term.update(new_value)


def is_term(obj) -> bool:
return isinstance(obj, Term)

Expand Down Expand Up @@ -509,32 +484,6 @@ def _disallow_scalar_only_bool_ops(self) -> None:
raise NotImplementedError("cannot evaluate scalar only bool ops")


class Div(BinOp):
"""
Div operator to special case casting.
Parameters
----------
lhs, rhs : Term or Op
The Terms or Ops in the ``/`` expression.
"""

def __init__(self, lhs, rhs) -> None:
super().__init__("/", lhs, rhs)

if not is_numeric_dtype(lhs.return_type) or not is_numeric_dtype(
rhs.return_type
):
raise TypeError(
f"unsupported operand type(s) for {self.op}: "
f"'{lhs.return_type}' and '{rhs.return_type}'"
)

# do not upcast float32s to float64 un-necessarily
acceptable_dtypes = [np.float32, np.float64]
_cast_inplace(com.flatten(self), acceptable_dtypes, np.float64)


UNARY_OPS_SYMS = ("+", "-", "~", "not")
_unary_ops_funcs = (operator.pos, operator.neg, operator.invert, operator.invert)
_unary_ops_dict = dict(zip(UNARY_OPS_SYMS, _unary_ops_funcs))
Expand Down

0 comments on commit ee4a2b6

Please sign in to comment.