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.
The expression to evaluate. This string cannot contain any Python
only Python expressions.
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
The engine used to evaluate the expression. Supported engines are
None : tries to use numexpr, falls back to python
numexpr for large speed ups in complex expressions
with large frames.
level python. This engine is generally not that useful.
More backends may be available in the future.
Whether to use true division, like in Python >= 3.
A dictionary of local variables, taken from locals() by default.
A dictionary of global variables, taken from globals() by default.
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
DataFrame.index and DataFrame.columns
variables that refer to their respective DataFrame
The number of prior stack frames to traverse and add to the current
scope. Most users will not need to change this parameter.
This is the target object for assignment. It is used when there is
variable assignment in the expression. If so, then target must
support item assignment with string keys, and if a copy is being
returned, it must also support .copy().
If target is provided, and the expression mutates target, whether
to modify target inplace. Otherwise, return a copy of target with
There are many instances where such an error can be raised:
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 a + 2.
inplace=True, but the expression is missing item assignment.
Item assignment is provided, but the target does not support
string item assignment.
Item assignment is provided and inplace=False, but the target
does not support the .copy() method
The dtype of any objects involved in an arithmetic % operation are
recursively cast to float64.
See the enhancing performance documentation for