From 59f4d88e1b46da5306fa98ff72a5d2ebdd333ae3 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Sun, 5 Jun 2022 22:11:25 -0400 Subject: [PATCH 1/2] ENH: Move NumExprClobberingError to error/__init__.py per GH27656 --- doc/source/reference/testing.rst | 1 + doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/core/computation/engines.py | 6 ++---- pandas/errors/__init__.py | 18 ++++++++++++++++++ pandas/tests/computation/test_eval.py | 10 +++++----- pandas/tests/frame/test_query_eval.py | 2 +- pandas/tests/test_errors.py | 1 + 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index 0e10b8792eaba..68e0555afc916 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -35,6 +35,7 @@ Exceptions and warnings errors.MergeError errors.NullFrequencyError errors.NumbaUtilError + errors.NumExprClobberingError errors.OptionError errors.OutOfBoundsDatetime errors.OutOfBoundsTimedelta diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index fddae3a4d26ec..d6fa6450aa1c4 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -151,7 +151,7 @@ Other enhancements - A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`) - Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`) - ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`) -- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, and :class:`SettingWithCopyWarning` are now exposed in ``pandas.errors`` (:issue:`27656`) +- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, and :class:`NumExprClobberingError` are now exposed in ``pandas.errors`` (:issue:`27656`) .. --------------------------------------------------------------------------- .. _whatsnew_150.notable_bug_fixes: diff --git a/pandas/core/computation/engines.py b/pandas/core/computation/engines.py index 0221256b2cbd8..c293839776e26 100644 --- a/pandas/core/computation/engines.py +++ b/pandas/core/computation/engines.py @@ -5,6 +5,8 @@ import abc +from pandas.errors import NumExprClobberingError + from pandas.core.computation.align import ( align_terms, reconstruct_object, @@ -20,10 +22,6 @@ _ne_builtins = frozenset(MATHOPS + REDUCTIONS) -class NumExprClobberingError(NameError): - pass - - def _check_ne_builtin_clash(expr: Expr) -> None: """ Attempt to prevent foot-shooting in a helpful way. diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 8a5840d0e5b3c..1cc6c010af804 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -308,3 +308,21 @@ class SettingWithCopyWarning(Warning): >>> df.loc[0:3]['A'] = 'a' # doctest: +SKIP ... # SettingWithCopyWarning: A value is trying to be set on a copy of a... """ + + +class NumExprClobberingError(NameError): + """ + Exception is raised when trying to use a built-in numexpr name as a variable name + in a method like query or eval. Eval will throw the error if the engine is set + to `numexpr'. 'numexpr' is the default engine value for eval if the numexpr package + is installed. + + Examples + -------- + >>> df = pd.DataFrame({'abs': [1, 1, 1]}) + >>> df.query("abs > 2") # doctest: +SKIP + ... # NumExprClobberingError: Variables in expression "(abs) > (2)" overlap... + >>> sin, a = 1, 2 + >>> pd.eval("sin + a", engine='numexpr') # doctest: +SKIP + ... # NumExprClobberingError: Variables in expression "(sin) + (a)" overlap... + """ diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index 0d91915ba2916..e70d493d23515 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -9,7 +9,10 @@ import numpy as np import pytest -from pandas.errors import PerformanceWarning +from pandas.errors import ( + NumExprClobberingError, + PerformanceWarning, +) import pandas.util._test_decorators as td from pandas.core.dtypes.common import ( @@ -27,10 +30,7 @@ ) import pandas._testing as tm from pandas.core.computation import pytables -from pandas.core.computation.engines import ( - ENGINES, - NumExprClobberingError, -) +from pandas.core.computation.engines import ENGINES import pandas.core.computation.expr as expr from pandas.core.computation.expr import ( BaseExprVisitor, diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index feb912b2420bf..fe3b04e8e27e6 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -533,7 +533,7 @@ def test_query_doesnt_pickup_local(self): df.query("sin > 5", engine=engine, parser=parser) def test_query_builtin(self): - from pandas.core.computation.engines import NumExprClobberingError + from pandas.errors import NumExprClobberingError engine, parser = self.engine, self.parser diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index 4cc40abb1b021..827c5767c514f 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -23,6 +23,7 @@ "SpecificationError", "SettingWithCopyError", "SettingWithCopyWarning", + "NumExprClobberingError", ], ) def test_exception_importable(exc): From 2557357add8f2d8b5842455d39d42f0cf460b930 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Mon, 6 Jun 2022 23:47:40 -0400 Subject: [PATCH 2/2] ENH: change backtick to single quote --- pandas/errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 1cc6c010af804..1918065d855a5 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -314,7 +314,7 @@ class NumExprClobberingError(NameError): """ Exception is raised when trying to use a built-in numexpr name as a variable name in a method like query or eval. Eval will throw the error if the engine is set - to `numexpr'. 'numexpr' is the default engine value for eval if the numexpr package + to 'numexpr'. 'numexpr' is the default engine value for eval if the numexpr package is installed. Examples