Skip to content

Commit daccc58

Browse files
author
Laurent Mutricy
committed
updating df.query and df.eval docstrings. resolves pandas-dev#16283
1 parent 4fb94bb commit daccc58

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pandas/core/frame.py

+33
Original file line numberDiff line numberDiff line change
@@ -4604,6 +4604,14 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
46044604
>>> df[df.B == df["C C"]]
46054605
A B C C
46064606
0 1 10 10
4607+
4608+
Using local variable:
4609+
4610+
>>> local_var = 2
4611+
>>> df.query("A <= @local_var")
4612+
A B C C
4613+
0 1 10 10
4614+
1 2 8 9
46074615
"""
46084616
inplace = validate_bool_kwarg(inplace, "inplace")
46094617
if not isinstance(expr, str):
@@ -4644,6 +4652,20 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
46444652
----------
46454653
expr : str
46464654
The expression string to evaluate.
4655+
4656+
You can refer to variables
4657+
in the environment by prefixing them with an '@' character like
4658+
``@a + b``.
4659+
4660+
You can refer to column names that are not valid Python variable names
4661+
by surrounding them in backticks. Thus, column names containing spaces
4662+
or punctuations (besides underscores) or starting with digits must be
4663+
surrounded by backticks. (For example, a column named "Area (cm^2)" would
4664+
be referenced as ```Area (cm^2)```). Column names which are Python keywords
4665+
(like "list", "for", "import", etc) cannot be used.
4666+
4667+
For example, if one of your columns is called ``a a`` and you want
4668+
to sum it with ``b``, your query should be ```a a` + b``.
46474669
inplace : bool, default False
46484670
If the expression contains an assignment, whether to perform the
46494671
operation inplace and mutate the existing DataFrame. Otherwise,
@@ -4723,6 +4745,17 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
47234745
2 3 6 9 -3
47244746
3 4 4 8 0
47254747
4 5 2 7 3
4748+
4749+
Local variables shall be explicitely referenced using ``@``
4750+
character in front of the name:
4751+
4752+
>>> local_var = 2
4753+
>>> df.eval("@local_var * A")
4754+
0 2
4755+
1 4
4756+
2 6
4757+
3 8
4758+
4 10
47264759
"""
47274760
from pandas.core.computation.eval import eval as _eval
47284761

0 commit comments

Comments
 (0)