Skip to content

Commit f623a6e

Browse files
Min max added to reduction ops (#50988)
* assertion error fix added in pandas/core/indexes/base.py * Added `min` and `max` as reduction operators * Added `test` and `whatsnew` - Added `test_query_numexpr_with_min_and_max_columns` in `pandas/frame/tests/test_query_eval.py` - Bug fix entry added in `pandas/doc/source/whatsnew/v2.0.0.rst` for issue `#50937` * Update doc/source/whatsnew/v2.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> * reimport error fixed --------- Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
1 parent f3015e3 commit f623a6e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ Numeric
10491049
- Bug in DataFrame reduction methods (e.g. :meth:`DataFrame.sum`) with object dtype, ``axis=1`` and ``numeric_only=False`` would not be coerced to float (:issue:`49551`)
10501050
- Bug in :meth:`DataFrame.sem` and :meth:`Series.sem` where an erroneous ``TypeError`` would always raise when using data backed by an :class:`ArrowDtype` (:issue:`49759`)
10511051
- Bug in :meth:`Series.__add__` casting to object for list and masked :class:`Series` (:issue:`22962`)
1052+
- Bug in :meth:`query` with ``engine="numexpr"`` and column names are ``min`` or ``max`` would raise a ``TypeError`` (:issue:`50937`)
10521053

10531054
Conversion
10541055
^^^^^^^^^^

pandas/core/computation/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
pprint_thing_encoded,
3636
)
3737

38-
REDUCTIONS = ("sum", "prod")
38+
REDUCTIONS = ("sum", "prod", "min", "max")
3939

4040
_unary_math_ops = (
4141
"sin",

pandas/tests/frame/test_query_eval.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.errors import UndefinedVariableError
6+
from pandas.errors import (
7+
NumExprClobberingError,
8+
UndefinedVariableError,
9+
)
710
import pandas.util._test_decorators as td
811

912
import pandas as pd
@@ -534,7 +537,6 @@ def test_query_doesnt_pickup_local(self):
534537
df.query("sin > 5", engine=engine, parser=parser)
535538

536539
def test_query_builtin(self):
537-
from pandas.errors import NumExprClobberingError
538540

539541
engine, parser = self.engine, self.parser
540542

@@ -864,6 +866,22 @@ def test_nested_scope(self):
864866
)
865867
tm.assert_frame_equal(expected, result)
866868

869+
def test_query_numexpr_with_min_and_max_columns(self):
870+
df = DataFrame({"min": [1, 2, 3], "max": [4, 5, 6]})
871+
regex_to_match = (
872+
r"Variables in expression \"\(min\) == \(1\)\" "
873+
r"overlap with builtins: \('min'\)"
874+
)
875+
with pytest.raises(NumExprClobberingError, match=regex_to_match):
876+
df.query("min == 1")
877+
878+
regex_to_match = (
879+
r"Variables in expression \"\(max\) == \(1\)\" "
880+
r"overlap with builtins: \('max'\)"
881+
)
882+
with pytest.raises(NumExprClobberingError, match=regex_to_match):
883+
df.query("max == 1")
884+
867885

868886
class TestDataFrameQueryPythonPandas(TestDataFrameQueryNumExprPandas):
869887
@classmethod

0 commit comments

Comments
 (0)