@@ -138,7 +138,7 @@ def _check_for_locals(expr, stack_level, parser):
138
138
139
139
def eval (expr , parser = 'pandas' , engine = 'numexpr' , truediv = True ,
140
140
local_dict = None , global_dict = None , resolvers = (), level = 0 ,
141
- target = None ):
141
+ target = None , assignment_allowed = True ):
142
142
"""Evaluate a Python expression as a string using various backends.
143
143
144
144
The following arithmetic operations are supported: ``+``, ``-``, ``*``,
@@ -196,6 +196,9 @@ def eval(expr, parser='pandas', engine='numexpr', truediv=True,
196
196
scope. Most users will **not** need to change this parameter.
197
197
target : a target object for assignment, optional, default is None
198
198
essentially this is a passed in resolver
199
+ assignment_allowed : bool
200
+ Whether the eval should be able to modify the input through
201
+ assigment.
199
202
200
203
Returns
201
204
-------
@@ -236,7 +239,11 @@ def eval(expr, parser='pandas', engine='numexpr', truediv=True,
236
239
237
240
# assign if needed
238
241
if env .target is not None and parsed_expr .assigner is not None :
239
- env .target [parsed_expr .assigner ] = ret
240
- return None
242
+ if assignment_allowed :
243
+ env .target [parsed_expr .assigner ] = ret
244
+ return None
245
+ else :
246
+ raise ValueError ("Expression includes assignment statement: \n "
247
+ "\t not allowed from DataFrame.query" )
241
248
242
249
return ret
0 commit comments