@@ -2034,6 +2034,67 @@ def test_query_on_column_names_with_special_characters(col1, col2, expr):
2034
2034
tm .assert_frame_equal (result , expected )
2035
2035
2036
2036
2037
+ def test_query_on_expr_with_no_backticks ():
2038
+ # GH 59285
2039
+ df = DataFrame (("aaa" , "vvv" , "zzz" ), columns = ["column_name" ])
2040
+ result = df .query ("'value' < column_name" )
2041
+ expected = df ["value" < df ["column_name" ]]
2042
+ tm .assert_frame_equal (result , expected )
2043
+
2044
+
2045
+ def test_query_on_expr_with_no_quotes_and_backtick_is_unmatched ():
2046
+ # GH 59285
2047
+ df = DataFrame ((1 , 5 , 10 ), columns = ["column-name" ])
2048
+ with pytest .raises (SyntaxError , match = "invalid syntax" ):
2049
+ df .query ("5 < `column-name" )
2050
+
2051
+
2052
+ def test_query_on_expr_with_no_quotes_and_backtick_is_matched ():
2053
+ # GH 59285
2054
+ df = DataFrame ((1 , 5 , 10 ), columns = ["column-name" ])
2055
+ result = df .query ("5 < `column-name`" )
2056
+ expected = df [5 < df ["column-name" ]]
2057
+ tm .assert_frame_equal (result , expected )
2058
+
2059
+
2060
+ def test_query_on_expr_with_backtick_opened_before_quote_and_backtick_is_unmatched ():
2061
+ # GH 59285
2062
+ df = DataFrame ((1 , 5 , 10 ), columns = ["It's" ])
2063
+ with pytest .raises (SyntaxError , match = "unterminated string literal" ):
2064
+ df .query ("5 < `It's" )
2065
+
2066
+
2067
+ def test_query_on_expr_with_backtick_opened_before_quote_and_backtick_is_matched ():
2068
+ # GH 59285
2069
+ df = DataFrame ((1 , 5 , 10 ), columns = ["It's" ])
2070
+ result = df .query ("5 < `It's`" )
2071
+ expected = df [5 < df ["It's" ]]
2072
+ tm .assert_frame_equal (result , expected )
2073
+
2074
+
2075
+ def test_query_on_expr_with_quote_opened_before_backtick_and_quote_is_unmatched ():
2076
+ # GH 59285
2077
+ df = DataFrame (("aaa" , "vvv" , "zzz" ), columns = ['It`s that\\ \' s "quote" #hash' ])
2078
+ with pytest .raises (SyntaxError , match = "unterminated string literal" ):
2079
+ df .query ("`column-name` < 'It`s that\\ 's \" quote\" #hash" )
2080
+
2081
+
2082
+ def test_query_on_expr_with_quote_opened_before_backtick_and_quote_is_matched_at_end ():
2083
+ # GH 59285
2084
+ df = DataFrame (("aaa" , "vvv" , "zzz" ), columns = ["column-name" ])
2085
+ result = df .query ("`column-name` < 'It`s that\\ 's \" quote\" #hash'" )
2086
+ expected = df [df ["column-name" ] < 'It`s that\' s "quote" #hash' ]
2087
+ tm .assert_frame_equal (result , expected )
2088
+
2089
+
2090
+ def test_query_on_expr_with_quote_opened_before_backtick_and_quote_is_matched_in_mid ():
2091
+ # GH 59285
2092
+ df = DataFrame (("aaa" , "vvv" , "zzz" ), columns = ["column-name" ])
2093
+ result = df .query ("'It`s that\\ 's \" quote\" #hash' < `column-name`" )
2094
+ expected = df ['It`s that\' s "quote" #hash' < df ["column-name" ]]
2095
+ tm .assert_frame_equal (result , expected )
2096
+
2097
+
2037
2098
def test_set_inplace ():
2038
2099
# https://github.com/pandas-dev/pandas/issues/47449
2039
2100
# Ensure we don't only update the DataFrame inplace, but also the actual
0 commit comments