-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: allow in-line expression assignment with df.eval #5343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
from pandas.computation.ops import (_binary_ops_dict, _unary_ops_dict, | ||
_special_case_arith_ops_syms, | ||
_arith_ops_syms, _bool_ops_syms) | ||
from pandas.computation.common import NameResolutionError | ||
import pandas.computation.expr as expr | ||
import pandas.util.testing as tm | ||
from pandas.util.testing import (assert_frame_equal, randbool, | ||
|
@@ -1151,9 +1152,65 @@ def test_assignment_fails(self): | |
df = DataFrame(np.random.randn(5, 3), columns=list('abc')) | ||
df2 = DataFrame(np.random.randn(5, 3)) | ||
expr1 = 'df = df2' | ||
self.assertRaises(NotImplementedError, self.eval, expr1, | ||
self.assertRaises(ValueError, self.eval, expr1, | ||
local_dict={'df': df, 'df2': df2}) | ||
|
||
def test_assignment_column(self): | ||
skip_if_no_ne('numexpr') | ||
df = DataFrame(np.random.randn(5, 2), columns=list('ab')) | ||
orig_df = df.copy() | ||
|
||
# multiple assignees | ||
self.assertRaises(SyntaxError, df.eval, 'd c = a + b') | ||
|
||
# invalid assignees | ||
self.assertRaises(SyntaxError, df.eval, 'd,c = a + b') | ||
self.assertRaises(SyntaxError, df.eval, 'Timestamp("20131001") = a + b') | ||
|
||
# single assignment - existing variable | ||
expected = orig_df.copy() | ||
expected['a'] = expected['a'] + expected['b'] | ||
df = orig_df.copy() | ||
df.eval('a = a + b') | ||
assert_frame_equal(df,expected) | ||
|
||
# single assignment - new variable | ||
expected = orig_df.copy() | ||
expected['c'] = expected['a'] + expected['b'] | ||
df = orig_df.copy() | ||
df.eval('c = a + b') | ||
assert_frame_equal(df,expected) | ||
|
||
# with a local name overlap | ||
def f(): | ||
df = orig_df.copy() | ||
a = 1 | ||
df.eval('a = 1 + b') | ||
return df | ||
|
||
df = f() | ||
expected = orig_df.copy() | ||
expected['a'] = 1 + expected['b'] | ||
assert_frame_equal(df,expected) | ||
|
||
df = orig_df.copy() | ||
def f(): | ||
a = 1 | ||
df.eval('a=a+b') | ||
self.assertRaises(NameResolutionError, f) | ||
|
||
# multiple assignment | ||
df = orig_df.copy() | ||
df.eval('c = a + b') | ||
self.assertRaises(SyntaxError, df.eval, 'c = a = b') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cpcloud did you mean something additional besides this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh nope! sorry didn't see that 👍 |
||
|
||
# explicit targets | ||
df = orig_df.copy() | ||
self.eval('c = df.a + df.b', local_dict={'df' : df}, target=df) | ||
expected = orig_df.copy() | ||
expected['c'] = expected['a'] + expected['b'] | ||
assert_frame_equal(df,expected) | ||
|
||
def test_basic_period_index_boolean_expression(self): | ||
df = mkdf(2, 2, data_gen_f=f, c_idx_type='p', r_idx_type='i') | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be switched with test right below? This is not valid Python syntax while the one below is valid Python syntax, but not valid
eval
syntax.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll check but iirc it fails in the assign block with a left hand side that is a list with a length of 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange ... that should simply throw the usual syntax error and not do any parsing outside of Python
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right
'd c = a + b'
raises a SyntaxError in fix_missing_locations....I think I meant thea = b = c
, which ends up having multiple assignment nodes (which I raise a Syntax Error); though in theory in the future you could handleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u change the test to make sure the multiple assignment fails? again not meaning to be a troll....just want to make sure the correct error is checked