Skip to content

Commit 12735f9

Browse files
committed
Merge pull request #7303 from cpcloud/eval-local-string-fix-7300
BUG: do not remove temporaries from eval/query scope
2 parents 069f144 + 83452eb commit 12735f9

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

doc/source/v0.14.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ Bug Fixes
7171
and ``left`` keyword is specified (:issue:`7226`)
7272
- BUG in ``DataFrame.hist`` raises ``TypeError`` when it contains non numeric column (:issue:`7277`)
7373
- BUG in ``Index.delete`` does not preserve ``name`` and ``freq`` attributes (:issue:`7302`)
74+
- Bug in ``DataFrame.query()``/``eval`` where local string variables with the @
75+
sign were being treated as temporaries attempting to be deleted
76+
(:issue:`7300`).

pandas/computation/expr.py

-2
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,10 @@ def _rewrite_membership_op(self, node, left, right):
340340
# pop the string variable out of locals and replace it with a list
341341
# of one string, kind of a hack
342342
if right_str:
343-
self.env.remove_tmp(right.name)
344343
name = self.env.add_tmp([right.value])
345344
right = self.term_type(name, self.env)
346345

347346
if left_str:
348-
self.env.remove_tmp(left.name)
349347
name = self.env.add_tmp([left.value])
350348
left = self.term_type(name, self.env)
351349

pandas/computation/scope.py

-10
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,6 @@ def add_tmp(self, value):
278278
# only increment if the variable gets put in the scope
279279
return name
280280

281-
def remove_tmp(self, name):
282-
"""Remove a temporary variable from this scope
283-
284-
Parameters
285-
----------
286-
name : str
287-
The name of a temporary to be removed
288-
"""
289-
del self.temps[name]
290-
291281
@property
292282
def ntemps(self):
293283
"""The number of temporary variables in this scope"""

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -13685,6 +13685,18 @@ def test_query_single_element_booleans(self):
1368513685
for parser, engine in product(PARSERS, ENGINES):
1368613686
yield self.check_query_single_element_booleans, parser, engine
1368713687

13688+
def check_query_string_scalar_variable(self, parser, engine):
13689+
tm.skip_if_no_ne(engine)
13690+
df = pd.DataFrame({'Symbol': ['BUD US', 'BUD US', 'IBM US', 'IBM US'],
13691+
'Price': [109.70, 109.72, 183.30, 183.35]})
13692+
e = df[df.Symbol == 'BUD US']
13693+
symb = 'BUD US'
13694+
r = df.query('Symbol == @symb', parser=parser, engine=engine)
13695+
tm.assert_frame_equal(e, r)
13696+
13697+
def test_query_string_scalar_variable(self):
13698+
for parser, engine in product(['pandas'], ENGINES):
13699+
yield self.check_query_string_scalar_variable, parser, engine
1368813700

1368913701
class TestDataFrameEvalNumExprPandas(tm.TestCase):
1369013702

0 commit comments

Comments
 (0)