Skip to content
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

updating df.query and df.eval docstrings. resolves #16283 #58749

Merged
merged 6 commits into from
May 31, 2024
Merged
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
109 changes: 73 additions & 36 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4574,36 +4574,44 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
Examples
--------
>>> df = pd.DataFrame(
... {"A": range(1, 6), "B": range(10, 0, -2), "C C": range(10, 5, -1)}
... {"A": range(1, 6), "B": range(10, 0, -2), "C&C": range(10, 5, -1)}
... )
>>> df
A B C C
A B C&C
0 1 10 10
1 2 8 9
2 3 6 8
3 4 4 7
4 5 2 6
>>> df.query("A > B")
A B C C
A B C&C
4 5 2 6

The previous expression is equivalent to

>>> df[df.A > df.B]
A B C C
A B C&C
4 5 2 6

For columns with spaces in their name, you can use backtick quoting.

>>> df.query("B == `C C`")
A B C C
>>> df.query("B == `C&C`")
A B C&C
0 1 10 10

The previous expression is equivalent to

>>> df[df.B == df["C C"]]
A B C C
>>> df[df.B == df["C&C"]]
A B C&C
0 1 10 10

Using local variable:

>>> local_var = 2
>>> df.query("A <= @local_var")
A B C&C
0 1 10 10
1 2 8 9
"""
inplace = validate_bool_kwarg(inplace, "inplace")
if not isinstance(expr, str):
Expand Down Expand Up @@ -4644,6 +4652,13 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
----------
expr : str
The expression string to evaluate.

You can refer to variables
in the environment by prefixing them with an '@' character like
``@a + b``.

You can refer to column names that are not valid Python variable
names by surrounding them with backticks `````.
inplace : bool, default False
If the expression contains an assignment, whether to perform the
operation inplace and mutate the existing DataFrame. Otherwise,
Expand Down Expand Up @@ -4675,14 +4690,16 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:

Examples
--------
>>> df = pd.DataFrame({"A": range(1, 6), "B": range(10, 0, -2)})
>>> df = pd.DataFrame(
... {"A": range(1, 6), "B": range(10, 0, -2), "C&C": range(10, 5, -1)}
... )
>>> df
A B
0 1 10
1 2 8
2 3 6
3 4 4
4 5 2
A B C&C
0 1 10 10
1 2 8 9
2 3 6 8
3 4 4 7
4 5 2 6
>>> df.eval("A + B")
0 11
1 10
Expand All @@ -4694,35 +4711,55 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
Assignment is allowed though by default the original DataFrame is not
modified.

>>> df.eval("C = A + B")
A B C
0 1 10 11
1 2 8 10
2 3 6 9
3 4 4 8
4 5 2 7
>>> df.eval("D = A + B")
A B C&C D
0 1 10 10 11
1 2 8 9 10
2 3 6 8 9
3 4 4 7 8
4 5 2 6 7
>>> df
A B
0 1 10
1 2 8
2 3 6
3 4 4
4 5 2
A B C&C
0 1 10 10
1 2 8 9
2 3 6 8
3 4 4 7
4 5 2 6

Multiple columns can be assigned to using multi-line expressions:

>>> df.eval(
... '''
... C = A + B
... D = A - B
... D = A + B
... E = A - B
... '''
... )
A B C D
0 1 10 11 -9
1 2 8 10 -6
2 3 6 9 -3
3 4 4 8 0
4 5 2 7 3
A B C&C D E
0 1 10 10 11 -9
1 2 8 9 10 -6
2 3 6 8 9 -3
3 4 4 7 8 0
4 5 2 6 7 3

For columns with spaces in their name, you can use backtick quoting.

>>> df.eval("B * `C&C`")
0 100
1 72
2 48
3 28
4 12

Local variables shall be explicitly referenced using ``@``
character in front of the name:

>>> local_var = 2
>>> df.eval("@local_var * A")
0 2
1 4
2 6
3 8
4 10
"""
from pandas.core.computation.eval import eval as _eval

Expand Down