Skip to content

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

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 6 commits into from
May 31, 2024
Merged
Changes from 3 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
94 changes: 69 additions & 25 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4604,6 +4604,14 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
>>> 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,20 @@ 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 in backticks. Thus, column names containing spaces
or punctuations (besides underscores) or starting with digits must be
surrounded by backticks. (For example, a column named "Area (cm^2)" would
be referenced as ```Area (cm^2)```). Column names which are Python keywords
(like "list", "for", "import", etc) cannot be used.

For example, if one of your columns is called ``a a`` and you want
to sum it with ``b``, your query should be ```a a` + b``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you demonstrate this in the Example section instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added one example in the Example section. Note that above wording is coming from query function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove all this explanation now that it's explained in the examples section

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 +4697,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 @@ -4695,19 +4719,19 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
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
A B C C C
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These headers don't look correct anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked and it looks correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the examples here you can see 5 column labels for 4 columns: https://pandas.pydata.org/preview/pandas-dev/pandas/58749/docs/reference/api/pandas.DataFrame.eval.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial example dataframe contains 3 columns: A, B and C C. The eval method will add a fourth column named C in the resulting dataframe for this example.
Repr of the dataframe does not make the separation of columns obvious when columns contains spaces. It is like there is 3 columns C when you actually have C C and C.

To make the example cristal clear I may change C C by C&C and update the examples to add columns with names differents than C.

Do you want me to update also query examples as C C is actually coming from there ??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the example cristal clear I may change C C by C&C and update the examples to add columns with names differents than C.

Ah yes that would be great. Sorry I didn't backtrack to see the context of the original columns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall I update query as well ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

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:

Expand All @@ -4717,12 +4741,32 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
... D = 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 C D
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
Loading