8000 ENH: Quoting column names containing spaces with backticks to use them in query and eval. by hwalinga · Pull Request #24955 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Quoting column names containing spaces with backticks to use them in query and eval. #24955

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 14 commits into from
Mar 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
TST: Add tests for backtick quoting (#6508)
  • Loading branch information
hwalinga committed Feb 15, 2019
commit ff463cac5a17c7518c12e68e24fa4e6ff4d1383b
31 changes: 31 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,34 @@ def test_invalid_type_for_operator_raises(self, parser, engine, op):

with pytest.raises(TypeError, match=msg):
df.eval('a {0} b'.format(op), engine=engine, parser=parser)


class TestDataFrameQueryBacktickQuoting(object):

def setup_method(self, method):
self.df = DataFrame({'A': [1, 2, 3],
'B B': [3, 2, 1],
'C C': [4, 5, 6]})

def teardown_method(self, method):
del self.df

def test_single_backtick_variable_query(self):
res = self.df.query('1 < `B B`')
expect = self.df[1 < self.df['B B']]
assert_frame_equal(res, expect)

def test_two_backtick_variables_query(self):
res = self.df.query('1 < `B B` and 4 < `C C`')
expect = self.df[(1 < self.df['B B']) & (4 < self.df['C C'])]
assert_frame_equal(res, expect)

def test_single_backtick_variable_expr(self):
res = self.df.eval('A + `B B`')
expect = self.df['A'] + self.df['B B']
assert_series_equal(res, expect)

def test_two_backtick_variables_expr(self):
res = self.df.eval('`B B` + `C C`')
expect = self.df['B B'] + self.df['C C']
assert_series_equal(res, expect)
0