@@ -2494,34 +2494,41 @@ def query(self, expr, inplace=False, **kwargs):
2494
2494
return new_data
2495
2495
2496
2496
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.
2499
2503
2500
2504
Parameters
2501
2505
----------
2502
- expr : string
2506
+ expr : str
2503
2507
The expression string to evaluate.
2504
2508
inplace : bool, default False
2505
2509
If the expression contains an assignment, whether to perform the
2506
2510
operation inplace and mutate the existing DataFrame. Otherwise,
2507
2511
a new DataFrame is returned.
2508
2512
2509
- .. versionadded:: 0.18.0
2510
-
2513
+ .. versionadded:: 0.18.0.
2511
2514
kwargs : dict
2512
2515
See the documentation for :func:`~pandas.eval` for complete details
2513
2516
on the keyword arguments accepted by
2514
2517
:meth:`~pandas.DataFrame.query`.
2515
2518
2516
2519
Returns
2517
2520
-------
2518
- ret : ndarray, scalar, or pandas object
2521
+ ndarray, scalar, or pandas object
2522
+ The result of the evaluation.
2519
2523
2520
2524
See Also
2521
2525
--------
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.
2525
2532
2526
2533
Notes
2527
2534
-----
@@ -2531,11 +2538,50 @@ def eval(self, expr, inplace=False, **kwargs):
2531
2538
2532
2539
Examples
2533
2540
--------
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
2539
2585
"""
2540
2586
from pandas .core .computation .eval import eval as _eval
2541
2587
0 commit comments