pandas 1.4.2

NotesParametersRaisesReturns
eval(expr: 'str | BinOp', parser: 'str' = 'pandas', engine: 'str | None' = None, truediv=<no_default>, local_dict=None, global_dict=None, resolvers=(), level=0, target=None, inplace=False)

The following arithmetic operations are supported: + , - , * , / , ** , % , // (python engine only) along with the following boolean operations: | (or), & (and), and ~ (not). Additionally, the 'pandas' parser allows the use of and , or , and not with the same semantics as the corresponding bitwise operators. ~pandas.Series and ~pandas.DataFrame objects are supported and behave as they would with plain ol' Python evaluation.

Notes

The dtype of any objects involved in an arithmetic % operation are recursively cast to float64 .

See the enhancing performance <enhancingperf.eval> documentation for more details.

Parameters

expr : str

The expression to evaluate. This string cannot contain any Python :None:None:`statements <https://docs.python.org/3/reference/simple_stmts.html#simple-statements>`, only Python :None:None:`expressions <https://docs.python.org/3/reference/simple_stmts.html#expression-statements>`.

parser : {'pandas', 'python'}, default 'pandas'

The parser to use to construct the syntax tree from the expression. The default of 'pandas' parses code slightly different than standard Python. Alternatively, you can parse an expression using the 'python' parser to retain strict Python semantics. See the enhancing performance <enhancingperf.eval> documentation for more details.

engine : {'python', 'numexpr'}, default 'numexpr'

The engine used to evaluate the expression. Supported engines are

  • None

    None

  • 'python' : Performs operations as if you had eval 'd in top

    level python. This engine is generally not that useful.

More backends may be available in the future.

truediv : bool, optional

Whether to use true division, like in Python >= 3.

deprecated
local_dict : dict or None, optional

A dictionary of local variables, taken from locals() by default.

global_dict : dict or None, optional

A dictionary of global variables, taken from globals() by default.

resolvers : list of dict-like or None, optional

A list of objects implementing the __getitem__ special method that you can use to inject an additional collection of namespaces to use for variable lookup. For example, this is used in the ~DataFrame.query method to inject the DataFrame.index and DataFrame.columns variables that refer to their respective ~pandas.DataFrame instance attributes.

level : int, optional

The number of prior stack frames to traverse and add to the current scope. Most users will not need to change this parameter.

target : object, optional, default None

This is the target object for assignment. It is used when there is variable assignment in the expression. If so, then :None:None:`target` must support item assignment with string keys, and if a copy is being returned, it must also support .copy() .

inplace : bool, default False

If :None:None:`target` is provided, and the expression mutates :None:None:`target`, whether to modify :None:None:`target` inplace. Otherwise, return a copy of :None:None:`target` with the mutation.

Raises

ValueError

There are many instances where such an error can be raised:

  • :None:None:`target=None`, but the expression is multiline.

  • The expression is multiline, but not all them have item assignment. An example of such an arrangement is this:

    a = b + 1 a + 2

    Here, there are expressions on different lines, making it multiline, but the last line has no variable assigned to the output of :None:None:`a + 2`.

  • :None:None:`inplace=True`, but the expression is missing item assignment.

  • Item assignment is provided, but the :None:None:`target` does not support string item assignment.

  • Item assignment is provided and :None:None:`inplace=False`, but the :None:None:`target` does not support the .copy() method

Returns

ndarray, numeric scalar, DataFrame, Series, or None

The completion value of evaluating the given code or None if inplace=True .

Evaluate a Python expression as a string using various backends.

See Also

DataFrame.eval

Evaluate a string describing operations on DataFrame columns.

DataFrame.query

Evaluates a boolean expression to query the columns of a frame.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({"animal": ["dog", "pig"], "age": [10, 20]})
... df animal age 0 dog 10 1 pig 20

We can add a new column using pd.eval :

This example is valid syntax, but we were not able to check execution
>>> pd.eval("double_age = df.age * 2", target=df)
  animal  age  double_age
0    dog   10          20
1    pig   20          40
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/computation/eval.py#166
type: <class 'function'>
Commit: