pandas.eval

pandas.eval(expr, parser='pandas', engine='numexpr', truediv=True, local_dict=None, global_dict=None, resolvers=None, level=2)

Evaluate a Python expression as a string using various backends.

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. Series and DataFrame objects are supported and behave as they would with plain ol’ Python evaluation.

Parameters:

expr : str or unicode

The expression to evaluate. This string cannot contain any Python statements, only Python expressions.

parser : string, default ‘pandas’, {‘pandas’, ‘python’}

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 documentation for more details.

engine : string, default ‘numexpr’, {‘python’, ‘numexpr’}

The engine used to evaluate the expression. Supported engines are

  • 'numexpr': This default engine evaluates pandas objects using

    numexpr for large speed ups in complex expressions with large frames.

  • '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

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 query() method to inject the index and columns variables that refer to their respective 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.

Returns:

ndarray, numeric scalar, DataFrame, Series :

Notes

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

See the enhancing performance documentation for more details.