Skip to content

Commit 7e5282f

Browse files
DOC: Fix inconsistent and incomplete documentation of pandas.eval (#59855)
* Improve content and organisation of eval documentation * Link to pd.eval in pd.DataFrame.query * Correct name for `//` is floor division * Include arctan2 Co-authored-by: Xiao Yuan <[email protected]> --------- Co-authored-by: Xiao Yuan <[email protected]>
1 parent c5cfe5d commit 7e5282f

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

pandas/core/computation/eval.py

+28-9
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,6 @@ def eval(
188188
"""
189189
Evaluate a Python expression as a string using various backends.
190190
191-
The following arithmetic operations are supported: ``+``, ``-``, ``*``,
192-
``/``, ``**``, ``%``, ``//`` (python engine only) along with the following
193-
boolean operations: ``|`` (or), ``&`` (and), and ``~`` (not).
194-
Additionally, the ``'pandas'`` parser allows the use of :keyword:`and`,
195-
:keyword:`or`, and :keyword:`not` with the same semantics as the
196-
corresponding bitwise operators. :class:`~pandas.Series` and
197-
:class:`~pandas.DataFrame` objects are supported and behave as they would
198-
with plain ol' Python evaluation.
199-
200191
.. warning::
201192
202193
``eval`` can run arbitrary code which can make you vulnerable to code
@@ -210,6 +201,34 @@ def eval(
210201
<https://docs.python.org/3/reference/simple_stmts.html#simple-statements>`__,
211202
only Python `expressions
212203
<https://docs.python.org/3/reference/simple_stmts.html#expression-statements>`__.
204+
205+
By default, with the numexpr engine, the following operations are supported:
206+
207+
- Arthimetic operations: ``+``, ``-``, ``*``, ``/``, ``**``, ``%``
208+
- Boolean operations: ``|`` (or), ``&`` (and), and ``~`` (not)
209+
- Comparison operators: ``<``, ``<=``, ``==``, ``!=``, ``>=``, ``>``
210+
211+
Furthermore, the following mathematical functions are supported:
212+
213+
- Trigonometric: ``sin``, ``cos``, ``tan``, ``arcsin``, ``arccos``, \
214+
``arctan``, ``arctan2``, ``sinh``, ``cosh``, ``tanh``, ``arcsinh``, \
215+
``arccosh`` and ``arctanh``
216+
- Logarithms: ``log`` natural, ``log10`` base 10, ``log1p`` log(1+x)
217+
- Absolute Value ``abs``
218+
- Square root ``sqrt``
219+
- Exponential ``exp`` and Exponential minus one ``expm1``
220+
221+
See the numexpr engine `documentation
222+
<https://numexpr.readthedocs.io/en/latest/user_guide.html#supported-functions>`__
223+
for further function support details.
224+
225+
Using the ``'python'`` engine allows the use of native Python operators
226+
such as floor division ``//``, in addition to built-in and user-defined
227+
Python functions.
228+
229+
Additionally, the ``'pandas'`` parser allows the use of :keyword:`and`,
230+
:keyword:`or`, and :keyword:`not` with the same semantics as the
231+
corresponding bitwise operators.
213232
parser : {'pandas', 'python'}, default 'pandas'
214233
The parser to use to construct the syntax tree from the expression. The
215234
default of ``'pandas'`` parses code slightly different than standard

pandas/core/frame.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -4479,20 +4479,11 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
44794479
expr : str
44804480
The query string to evaluate.
44814481
4482-
You can refer to variables
4483-
in the environment by prefixing them with an '@' character like
4484-
``@a + b``.
4485-
4486-
You can refer to column names that are not valid Python variable names
4487-
by surrounding them in backticks. Thus, column names containing spaces
4488-
or punctuation (besides underscores) or starting with digits must be
4489-
surrounded by backticks. (For example, a column named "Area (cm^2)" would
4490-
be referenced as ```Area (cm^2)```). Column names which are Python keywords
4491-
(like "if", "for", "import", etc) cannot be used.
4492-
4493-
For example, if one of your columns is called ``a a`` and you want
4494-
to sum it with ``b``, your query should be ```a a` + b``.
4482+
See the documentation for :func:`eval` for details of
4483+
supported operations and functions in the query string.
44954484
4485+
See the documentation for :meth:`DataFrame.eval` for details on
4486+
referring to column names and variables in the query string.
44964487
inplace : bool
44974488
Whether to modify the DataFrame rather than creating a new one.
44984489
**kwargs
@@ -4651,16 +4642,26 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
46514642
in the environment by prefixing them with an '@' character like
46524643
``@a + b``.
46534644
4654-
You can refer to column names that are not valid Python variable
4655-
names by surrounding them with backticks `````.
4645+
You can refer to column names that are not valid Python variable names
4646+
by surrounding them in backticks. Thus, column names containing spaces
4647+
or punctuation (besides underscores) or starting with digits must be
4648+
surrounded by backticks. (For example, a column named "Area (cm^2)" would
4649+
be referenced as ```Area (cm^2)```). Column names which are Python keywords
4650+
(like "if", "for", "import", etc) cannot be used.
4651+
4652+
For example, if one of your columns is called ``a a`` and you want
4653+
to sum it with ``b``, your query should be ```a a` + b``.
4654+
4655+
See the documentation for :func:`eval` for full details of
4656+
supported operations and functions in the expression string.
46564657
inplace : bool, default False
46574658
If the expression contains an assignment, whether to perform the
46584659
operation inplace and mutate the existing DataFrame. Otherwise,
46594660
a new DataFrame is returned.
46604661
**kwargs
46614662
See the documentation for :func:`eval` for complete details
46624663
on the keyword arguments accepted by
4663-
:meth:`~pandas.DataFrame.query`.
4664+
:meth:`~pandas.DataFrame.eval`.
46644665
46654666
Returns
46664667
-------

0 commit comments

Comments
 (0)