Skip to content

Commit

Permalink
remove ops div class to solve pandas-dev#21374 (pandas-dev#59144)
Browse files Browse the repository at this point in the history
* remove core.computation.ops.Div resolves pandas-dev#21374 pandas-dev#58748

* need to preserve order

* updating tests

* update whatsnew

* solve mypy issue

* fixing pytests

* better than cast

* adding specific test

* Update pandas/tests/frame/test_query_eval.py

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>

* Update pandas/tests/computation/test_eval.py

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>

---------

Co-authored-by: Laurent Mutricy <laurent.mutricy@ekium.eu>
Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 27, 2024
1 parent acb9e97 commit e480752
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 64 deletions.
1 change: 1 addition & 0 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

COMPLEX_DTYPES: list[Dtype] = [complex, "complex64", "complex128"]
STRING_DTYPES: list[Dtype] = [str, "str", "U"]
COMPLEX_FLOAT_DTYPES: list[Dtype] = [*COMPLEX_DTYPES, *FLOAT_NUMPY_DTYPES]

DATETIME64_DTYPES: list[Dtype] = ["datetime64[ns]", "M8[ns]"]
TIMEDELTA64_DTYPES: list[Dtype] = ["timedelta64[ns]", "m8[ns]"]
Expand Down
15 changes: 15 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,21 @@ def complex_dtype(request):
return request.param


@pytest.fixture(params=tm.COMPLEX_FLOAT_DTYPES)
def complex_or_float_dtype(request):
"""
Parameterized fixture for complex and numpy float dtypes.
* complex
* 'complex64'
* 'complex128'
* float
* 'float32'
* 'float64'
"""
return request.param


@pytest.fixture(params=tm.SIGNED_INT_NUMPY_DTYPES)
def any_signed_int_numpy_dtype(request):
"""
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
UNARY_OPS_SYMS,
BinOp,
Constant,
Div,
FuncNode,
Op,
Term,
Expand Down Expand Up @@ -370,7 +369,7 @@ class BaseExprVisitor(ast.NodeVisitor):
"Add",
"Sub",
"Mult",
None,
"Div",
"Pow",
"FloorDiv",
"Mod",
Expand Down Expand Up @@ -533,9 +532,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
53 changes: 0 additions & 53 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,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 @@ -513,34 +488,6 @@ def _disallow_scalar_only_bool_ops(self):
raise NotImplementedError("cannot evaluate scalar only bool ops")


def isnumeric(dtype) -> bool:
return issubclass(np.dtype(dtype).type, np.number)


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 isnumeric(lhs.return_type) or not isnumeric(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
22 changes: 16 additions & 6 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,16 +747,26 @@ class TestTypeCasting:
@pytest.mark.parametrize("op", ["+", "-", "*", "**", "/"])
# maybe someday... numexpr has too many upcasting rules now
# chain(*(np.core.sctypes[x] for x in ['uint', 'int', 'float']))
@pytest.mark.parametrize("dt", [np.float32, np.float64])
@pytest.mark.parametrize("left_right", [("df", "3"), ("3", "df")])
def test_binop_typecasting(self, engine, parser, op, dt, left_right):
df = DataFrame(np.random.default_rng(2).standard_normal((5, 3)), dtype=dt)
def test_binop_typecasting(
self, engine, parser, op, complex_or_float_dtype, left_right, request
):
# GH#21374
dtype = complex_or_float_dtype
df = DataFrame(np.random.default_rng(2).standard_normal((5, 3)), dtype=dtype)
left, right = left_right
s = f"{left} {op} {right}"
res = pd.eval(s, engine=engine, parser=parser)
assert df.values.dtype == dt
assert res.values.dtype == dt
tm.assert_frame_equal(res, eval(s))
if dtype == "complex64" and engine == "numexpr":
mark = pytest.mark.xfail(
reason="numexpr issue with complex that are upcast "
"to complex 128 "
"https://github.com/pydata/numexpr/issues/492"
)
request.applymarker(mark)
assert df.values.dtype == dtype
assert res.values.dtype == dtype
tm.assert_frame_equal(res, eval(s), check_exact=False)


# -------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,39 @@ def test_eval_object_dtype_binop(self):
expected = DataFrame({"a1": ["Y", "N"], "c": [True, False]})
tm.assert_frame_equal(res, expected)

def test_using_numpy(self, engine, parser):
# GH 58041
skip_if_no_pandas_parser(parser)
df = Series([0.2, 1.5, 2.8], name="a").to_frame()
res = df.eval("@np.floor(a)", engine=engine, parser=parser)
expected = np.floor(df["a"])
tm.assert_series_equal(expected, res)

def test_eval_simple(self, engine, parser):
df = Series([0.2, 1.5, 2.8], name="a").to_frame()
res = df.eval("a", engine=engine, parser=parser)
expected = df["a"]
tm.assert_series_equal(expected, res)

def test_extension_array_eval(self, engine, parser, request):
# GH#58748
if engine == "numexpr":
mark = pytest.mark.xfail(
reason="numexpr does not support extension array dtypes"
)
request.applymarker(mark)
df = DataFrame({"a": pd.array([1, 2, 3]), "b": pd.array([4, 5, 6])})
result = df.eval("a / b", engine=engine, parser=parser)
expected = Series(pd.array([0.25, 0.40, 0.50]))
tm.assert_series_equal(result, expected)

def test_complex_eval(self, engine, parser):
# GH#21374
df = DataFrame({"a": [1 + 2j], "b": [1 + 1j]})
result = df.eval("a/b", engine=engine, parser=parser)
expected = Series([1.5 + 0.5j])
tm.assert_series_equal(result, expected)


class TestDataFrameQueryWithMultiIndex:
def test_query_with_named_multiindex(self, parser, engine):
Expand Down

0 comments on commit e480752

Please sign in to comment.