Skip to content

Commit 3c8d1d4

Browse files
StephenVolandTomAugspurger
authored andcommitted
DOC: update the DataFrame.eval() docstring (#20209)
1 parent 45f9e57 commit 3c8d1d4

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

pandas/core/frame.py

+60-14
Original file line numberDiff line numberDiff line change
@@ -2494,34 +2494,41 @@ def query(self, expr, inplace=False, **kwargs):
24942494
return new_data
24952495

24962496
def eval(self, expr, inplace=False, **kwargs):
2497-
"""Evaluate an expression in the context of the calling DataFrame
2498-
instance.
2497+
"""
2498+
Evaluate a string describing operations on DataFrame columns.
2499+
2500+
Operates on columns only, not specific rows or elements. This allows
2501+
`eval` to run arbitrary code, which can make you vulnerable to code
2502+
injection if you pass user input to this function.
24992503
25002504
Parameters
25012505
----------
2502-
expr : string
2506+
expr : str
25032507
The expression string to evaluate.
25042508
inplace : bool, default False
25052509
If the expression contains an assignment, whether to perform the
25062510
operation inplace and mutate the existing DataFrame. Otherwise,
25072511
a new DataFrame is returned.
25082512
2509-
.. versionadded:: 0.18.0
2510-
2513+
.. versionadded:: 0.18.0.
25112514
kwargs : dict
25122515
See the documentation for :func:`~pandas.eval` for complete details
25132516
on the keyword arguments accepted by
25142517
:meth:`~pandas.DataFrame.query`.
25152518
25162519
Returns
25172520
-------
2518-
ret : ndarray, scalar, or pandas object
2521+
ndarray, scalar, or pandas object
2522+
The result of the evaluation.
25192523
25202524
See Also
25212525
--------
2522-
pandas.DataFrame.query
2523-
pandas.DataFrame.assign
2524-
pandas.eval
2526+
DataFrame.query : Evaluates a boolean expression to query the columns
2527+
of a frame.
2528+
DataFrame.assign : Can evaluate an expression or function to create new
2529+
values for a column.
2530+
pandas.eval : Evaluate a Python expression as a string using various
2531+
backends.
25252532
25262533
Notes
25272534
-----
@@ -2531,11 +2538,50 @@ def eval(self, expr, inplace=False, **kwargs):
25312538
25322539
Examples
25332540
--------
2534-
>>> from numpy.random import randn
2535-
>>> from pandas import DataFrame
2536-
>>> df = pd.DataFrame(randn(10, 2), columns=list('ab'))
2537-
>>> df.eval('a + b')
2538-
>>> df.eval('c = a + b')
2541+
>>> df = pd.DataFrame({'A': range(1, 6), 'B': range(10, 0, -2)})
2542+
>>> df
2543+
A B
2544+
0 1 10
2545+
1 2 8
2546+
2 3 6
2547+
3 4 4
2548+
4 5 2
2549+
>>> df.eval('A + B')
2550+
0 11
2551+
1 10
2552+
2 9
2553+
3 8
2554+
4 7
2555+
dtype: int64
2556+
2557+
Assignment is allowed though by default the original DataFrame is not
2558+
modified.
2559+
2560+
>>> df.eval('C = A + B')
2561+
A B C
2562+
0 1 10 11
2563+
1 2 8 10
2564+
2 3 6 9
2565+
3 4 4 8
2566+
4 5 2 7
2567+
>>> df
2568+
A B
2569+
0 1 10
2570+
1 2 8
2571+
2 3 6
2572+
3 4 4
2573+
4 5 2
2574+
2575+
Use ``inplace=True`` to modify the original DataFrame.
2576+
2577+
>>> df.eval('C = A + B', inplace=True)
2578+
>>> df
2579+
A B C
2580+
0 1 10 11
2581+
1 2 8 10
2582+
2 3 6 9
2583+
3 4 4 8
2584+
4 5 2 7
25392585
"""
25402586
from pandas.core.computation.eval import eval as _eval
25412587

0 commit comments

Comments
 (0)