Skip to content

Commit c3ac531

Browse files
authored
ENH: Move NumExprClobberingError to error/__init__.py per GH27656 (#47253)
* ENH: Move NumExprClobberingError to error/__init__.py per GH27656 * ENH: change backtick to single quote
1 parent 4ea3efe commit c3ac531

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

doc/source/reference/testing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Exceptions and warnings
3535
errors.MergeError
3636
errors.NullFrequencyError
3737
errors.NumbaUtilError
38+
errors.NumExprClobberingError
3839
errors.OptionError
3940
errors.OutOfBoundsDatetime
4041
errors.OutOfBoundsTimedelta

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Other enhancements
151151
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
152152
- 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`)
153153
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)
154-
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, and :class:`SettingWithCopyWarning` are now exposed in ``pandas.errors`` (:issue:`27656`)
154+
- :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, :class:`SettingWithCopyWarning`, and :class:`NumExprClobberingError` are now exposed in ``pandas.errors`` (:issue:`27656`)
155155
- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`)
156156

157157
.. ---------------------------------------------------------------------------

pandas/core/computation/engines.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import abc
77

8+
from pandas.errors import NumExprClobberingError
9+
810
from pandas.core.computation.align import (
911
align_terms,
1012
reconstruct_object,
@@ -20,10 +22,6 @@
2022
_ne_builtins = frozenset(MATHOPS + REDUCTIONS)
2123

2224

23-
class NumExprClobberingError(NameError):
24-
pass
25-
26-
2725
def _check_ne_builtin_clash(expr: Expr) -> None:
2826
"""
2927
Attempt to prevent foot-shooting in a helpful way.

pandas/errors/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,21 @@ class SettingWithCopyWarning(Warning):
308308
>>> df.loc[0:3]['A'] = 'a' # doctest: +SKIP
309309
... # SettingWithCopyWarning: A value is trying to be set on a copy of a...
310310
"""
311+
312+
313+
class NumExprClobberingError(NameError):
314+
"""
315+
Exception is raised when trying to use a built-in numexpr name as a variable name
316+
in a method like query or eval. Eval will throw the error if the engine is set
317+
to 'numexpr'. 'numexpr' is the default engine value for eval if the numexpr package
318+
is installed.
319+
320+
Examples
321+
--------
322+
>>> df = pd.DataFrame({'abs': [1, 1, 1]})
323+
>>> df.query("abs > 2") # doctest: +SKIP
324+
... # NumExprClobberingError: Variables in expression "(abs) > (2)" overlap...
325+
>>> sin, a = 1, 2
326+
>>> pd.eval("sin + a", engine='numexpr') # doctest: +SKIP
327+
... # NumExprClobberingError: Variables in expression "(sin) + (a)" overlap...
328+
"""

pandas/tests/computation/test_eval.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import numpy as np
1010
import pytest
1111

12-
from pandas.errors import PerformanceWarning
12+
from pandas.errors import (
13+
NumExprClobberingError,
14+
PerformanceWarning,
15+
)
1316
import pandas.util._test_decorators as td
1417

1518
from pandas.core.dtypes.common import (
@@ -27,10 +30,7 @@
2730
)
2831
import pandas._testing as tm
2932
from pandas.core.computation import pytables
30-
from pandas.core.computation.engines import (
31-
ENGINES,
32-
NumExprClobberingError,
33-
)
33+
from pandas.core.computation.engines import ENGINES
3434
import pandas.core.computation.expr as expr
3535
from pandas.core.computation.expr import (
3636
BaseExprVisitor,

pandas/tests/frame/test_query_eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def test_query_doesnt_pickup_local(self):
533533
df.query("sin > 5", engine=engine, parser=parser)
534534

535535
def test_query_builtin(self):
536-
from pandas.core.computation.engines import NumExprClobberingError
536+
from pandas.errors import NumExprClobberingError
537537

538538
engine, parser = self.engine, self.parser
539539

pandas/tests/test_errors.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"SpecificationError",
2424
"SettingWithCopyError",
2525
"SettingWithCopyWarning",
26+
"NumExprClobberingError",
2627
],
2728
)
2829
def test_exception_importable(exc):

0 commit comments

Comments
 (0)