-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: update the DataFrame.eval() docstring #20209
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 1 commit
3e8ff19
c86dc09
b67e76c
b48d7c6
f45aebe
194bdc2
b1301d8
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 |
---|---|---|
|
@@ -2375,34 +2375,41 @@ def query(self, expr, inplace=False, **kwargs): | |
return new_data | ||
|
||
def eval(self, expr, inplace=False, **kwargs): | ||
"""Evaluate an expression in the context of the calling DataFrame | ||
instance. | ||
""" | ||
Evaluate expression in the context of the calling DataFrame instance. | ||
|
||
Evaluates a string describing operations on dataframe columns. This | ||
allows `eval` to run arbitrary code, but remember to sanitize your | ||
inputs. | ||
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. Might be useful to state clearly that this will not evaluate expression within elements of the DataFrame, that it will only evaluate columns. |
||
|
||
Parameters | ||
---------- | ||
expr : string | ||
expr : str | ||
The expression string to evaluate. | ||
inplace : bool, default False | ||
If the expression contains an assignment, whether to perform the | ||
operation inplace and mutate the existing DataFrame. Otherwise, | ||
a new DataFrame is returned. | ||
|
||
.. versionadded:: 0.18.0 | ||
|
||
.. versionadded:: 0.18.0. | ||
kwargs : dict | ||
See the documentation for :func:`~pandas.eval` for complete details | ||
on the keyword arguments accepted by | ||
:meth:`~pandas.DataFrame.query`. | ||
|
||
Returns | ||
------- | ||
ret : ndarray, scalar, or pandas object | ||
ndarray, scalar, or pandas object | ||
The result of the evaluation. | ||
|
||
See Also | ||
-------- | ||
pandas.DataFrame.query | ||
pandas.DataFrame.assign | ||
pandas.eval | ||
pandas.DataFrame.query : Evaluates a boolean expression to query the | ||
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 believe |
||
columns of a frame. | ||
pandas.DataFrame.assign : Can evaluate an expression or function to | ||
create new values for a column. | ||
pandas.eval : Evaluate a Python expression as a string using various | ||
backends. | ||
|
||
Notes | ||
----- | ||
|
@@ -2412,11 +2419,43 @@ def eval(self, expr, inplace=False, **kwargs): | |
|
||
Examples | ||
-------- | ||
>>> from numpy.random import randn | ||
>>> from pandas import DataFrame | ||
>>> df = pd.DataFrame(randn(10, 2), columns=list('ab')) | ||
>>> df.eval('a + b') | ||
>>> df.eval('c = a + b') | ||
>>> df = pd.DataFrame({'A': range(1, 6), 'B': range(10, 0, -2)}) | ||
>>> df | ||
A B | ||
0 1 10 | ||
1 2 8 | ||
2 3 6 | ||
3 4 4 | ||
4 5 2 | ||
>>> df.eval('A + B') | ||
0 11 | ||
1 10 | ||
2 9 | ||
3 8 | ||
4 7 | ||
dtype: int64 | ||
>>> df.eval('C = A + B') | ||
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. Could you add a line before this saying "Assignment is allowed and by default the original DataFrame is not modiefied." |
||
A B C | ||
0 1 10 11 | ||
1 2 8 10 | ||
2 3 6 9 | ||
3 4 4 8 | ||
4 5 2 7 | ||
>>> df | ||
A B | ||
0 1 10 | ||
1 2 8 | ||
2 3 6 | ||
3 4 4 | ||
4 5 2 | ||
>>> df.eval('C = A + B', inplace=True) | ||
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. And before this, say "use |
||
>>> df | ||
A B C | ||
0 1 10 11 | ||
1 2 8 10 | ||
2 3 6 9 | ||
3 4 4 8 | ||
4 5 2 7 | ||
""" | ||
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.
dataframe --> DataFrame