Skip to content

Commit 004c589

Browse files
escape backticks
1 parent c7d7134 commit 004c589

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandas/core/computation/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def create_valid_python_identifier(name: str) -> str:
5656
"$": "_DOLLARSIGN_",
5757
"€": "_EUROSIGN_",
5858
"°": "_DEGREESIGN_",
59-
# Including quotes works, but there are exceptions.
6059
"'": "_SINGLEQUOTE_",
6160
'"': "_DOUBLEQUOTE_",
6261
"#": "_HASH_",
@@ -128,6 +127,7 @@ def clean_column_name(name: Hashable) -> Hashable:
128127
which is not caught and propagates to the user level.
129128
"""
130129
try:
130+
name = name.replace("`", "``") # Escape backticks
131131
tokenized = tokenize_string(f"`{name}`")
132132
tokval = next(tokenized)[1]
133133
return create_valid_python_identifier(tokval)

pandas/tests/computation/test_eval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,14 @@ def test_query_on_expr_with_comment():
19941994
tm.assert_frame_equal(result, expected)
19951995

19961996

1997+
def test_query_on_expr_with_column_name_with_backtick_and_hash():
1998+
# GH 59285
1999+
df = DataFrame((1, 2, 3), columns=["a`#b"])
2000+
result = df.query("`a``#b` < 2")
2001+
expected = df[df["a`#b"] < 2]
2002+
tm.assert_frame_equal(result, expected)
2003+
2004+
19972005
def test_query_on_expr_with_string_with_backticks():
19982006
# GH 59285
19992007
df = DataFrame(("`", "`````", "``````````"), columns=["#backticks"])

0 commit comments

Comments
 (0)