@@ -4628,27 +4628,36 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4628
4628
return result
4629
4629
4630
4630
@overload
4631
- def eval (self , expr : str , * , inplace : Literal [False ] = ..., ** kwargs ) -> Any : ...
4632
4631
4633
- @overload
4634
- def eval (self , expr : str , * , inplace : Literal [True ], ** kwargs ) -> None : ...
4632
+ def eval (self , expr : str , * , inplace : bool = False , ** kwargs ) -> Any | None :
4633
+ """
4634
+ Evaluate a string describing operations on DataFrame columns.
4635
4635
4636
- def eval ( self , expr : str , * , inplace : bool = False , ** kwargs ) -> Any | None :
4637
- """
4638
- Evaluate a string describing operations on DataFrame columns .
4636
+ Operates on columns only, not specific rows or elements. This allows
4637
+ `eval` to run arbitrary code, which can make you vulnerable to code
4638
+ injection if you pass user input to this function .
4639
4639
4640
- Operates on columns only, not specific rows or elements. This allows
4641
- `eval` to run arbitrary code, which can make you vulnerable to code
4642
- injection if you pass user input to this function.
4640
+ Parameters
4641
+ ----------
4642
+ expr : str
4643
+ The expression string to evaluate.
4644
+ inplace : bool, default False
4645
+ If the expression contains an assignment, whether to perform the
4646
+ operation inplace and mutate the existing DataFrame. Otherwise,
4647
+ a new DataFrame is returned.
4648
+ """
4649
+ from pandas .core .computation .eval import eval as _eval
4650
+
4651
+ if not isinstance (expr , str ):
4652
+ msg = f"expr must be a string to be evaluated, { type (expr )} given"
4653
+ raise ValueError (msg )
4654
+
4655
+ # Ensure __finalize__ is called to propagate metadata
4656
+ result = super ().eval (expr , inplace = inplace , ** kwargs )
4657
+ if not inplace :
4658
+ result = result .__finalize__ (self , method = "eval" )
4659
+ return result
4643
4660
4644
- Parameters
4645
- ----------
4646
- expr : str
4647
- The expression string to evaluate.
4648
- inplace : bool, default False
4649
- If the expression contains an assignment, whether to perform the
4650
- operation inplace and mutate the existing DataFrame. Otherwise,
4651
- a new DataFrame is returned.
4652
4661
* * kwargs
4653
4662
See the documentation for :func :`eval` for complete details
4654
4663
on the keyword arguments accepted by
0 commit comments