-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
daccc58
0de174a
5e0e65b
6e9e58f
2a176f3
22d2c63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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``. | ||
inplace : bool, default False | ||
If the expression contains an assignment, whether to perform the | ||
operation inplace and mutate the existing DataFrame. Otherwise, | ||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These headers don't look correct anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I double checked and it looks correct There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initial example dataframe contains 3 columns: To make the example cristal clear I may change Do you want me to update also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah yes that would be great. Sorry I didn't backtrack to see the context of the original columns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall I update There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
||
|
@@ -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 | ||
|
||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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