Skip to content

Commit 7a5fa0c

Browse files
parametrize and add tests
1 parent 459bc76 commit 7a5fa0c

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

pandas/tests/computation/test_eval.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,45 +1994,27 @@ def test_query_on_expr_with_comment():
19941994
tm.assert_frame_equal(result, expected)
19951995

19961996

1997-
def test_query_on_column_names_with_single_quote_character():
1998-
df = DataFrame(
1999-
[
2000-
{"it's": 1, "that's": 2},
2001-
{"it's": 3, "that's": 4},
2002-
{"it's": -1, "that's": -2},
2003-
{"it's": -3, "that's": -4},
2004-
]
2005-
)
2006-
result = df.query("`it's` < `that's`")
2007-
expected = df[df["it's"] < df["that's"]]
2008-
tm.assert_frame_equal(result, expected)
2009-
2010-
2011-
def test_query_on_column_names_with_double_quote_character():
2012-
df = DataFrame(
2013-
[
2014-
{'it"s': 1, 'that"s': 2},
2015-
{'it"s': 3, 'that"s': 4},
2016-
{'it"s': -1, 'that"s': -2},
2017-
{'it"s': -3, 'that"s': -4},
2018-
]
2019-
)
2020-
result = df.query('`it"s` < `that"s`')
2021-
expected = df[df['it"s'] < df['that"s']]
2022-
tm.assert_frame_equal(result, expected)
2023-
2024-
2025-
def test_query_on_column_names_with_single_quote_and_double_quote_character():
1997+
@pytest.mark.parametrize(
1998+
"col1,col2,expr",
1999+
[
2000+
("it's", "that's", "`it's` < `that's`"),
2001+
('it"s', 'that"s', '`it"s` < `that"s`'),
2002+
("it's", 'that\'s "nice"', "`it's` < `that's \"nice\"`"),
2003+
("it's", "that's #cool", "`it's` < `that's #cool` # This is a comment"),
2004+
],
2005+
)
2006+
def test_query_on_column_names_with_special_characters(col1, col2, expr):
2007+
# GH 59285
20262008
df = DataFrame(
20272009
[
2028-
{"it's": 1, 'that\'s "nice"': 2},
2029-
{"it's": 3, 'that\'s "nice"': 4},
2030-
{"it's": -1, 'that\'s "nice"': -2},
2031-
{"it's": -3, 'that\'s "nice"': -4},
2010+
{col1: 1, col2: 2},
2011+
{col1: 3, col2: 4},
2012+
{col1: -1, col2: -2},
2013+
{col1: -3, col2: -4},
20322014
]
20332015
)
2034-
result = df.query("`it's` < `that's \"nice\"`")
2035-
expected = df[df["it's"] < df['that\'s "nice"']]
2016+
result = df.query(expr)
2017+
expected = df[df[col1] < df[col2]]
20362018
tm.assert_frame_equal(result, expected)
20372019

20382020

0 commit comments

Comments
 (0)