Skip to content

ENH: Move NumExprClobberingError to error/__init__.py per GH27656 #47253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/reference/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Exceptions and warnings
errors.MergeError
errors.NullFrequencyError
errors.NumbaUtilError
errors.NumExprClobberingError
errors.OptionError
errors.OutOfBoundsDatetime
errors.OutOfBoundsTimedelta
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`)

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/computation/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import abc

from pandas.errors import NumExprClobberingError

from pandas.core.computation.align import (
align_terms,
reconstruct_object,
Expand All @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
"""
10 changes: 5 additions & 5 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"SpecificationError",
"SettingWithCopyError",
"SettingWithCopyWarning",
"NumExprClobberingError",
],
)
def test_exception_importable(exc):
Expand Down