@@ -4555,8 +4555,9 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4555
4555
For other characters that fall outside the ASCII range (U+0001..U+007F)
4556
4556
and those that are not further specified in PEP 3131,
4557
4557
the query parser will raise an error.
4558
- This excludes whitespace different than the space character
4559
- and the backtick itself (backtick cannot be escaped).
4558
+ This excludes whitespace different than the space character,
4559
+ but also the hashtag (as it is used for comments) and the backtick
4560
+ itself (backtick can also not be escaped).
4560
4561
4561
4562
See also the `Python documentation about lexical analysis
4562
4563
<https://docs.python.org/3/reference/lexical_analysis.html>`__
@@ -4615,16 +4616,15 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4615
4616
if any (("#" in col ) or ("'" in col ) or ('"' in col ) for col in self .columns ):
4616
4617
# Create a copy of `self` with column names escaped
4617
4618
escaped_self = self .copy ()
4618
- escaped_self .columns = [
4619
- urllib .parse .quote (col ) for col in escaped_self .columns
4620
- ]
4619
+ escaped_self .columns = map (urllib .parse .quote , escaped_self .columns )
4621
4620
4622
4621
# In expr, escape column names between backticks
4623
- column_name_to_escaped_name = {
4622
+ column_name_to_escaped = {
4624
4623
col : urllib .parse .quote (col ) for col in self .columns
4625
4624
}
4625
+ # Odd-number indexes are column names
4626
4626
escaped_expr = "`" .join (
4627
- (column_name_to_escaped_name .get (token , token ) if (i % 2 ) else token )
4627
+ (column_name_to_escaped .get (token , token ) if (i % 2 ) else token )
4628
4628
for i , token in enumerate (expr .split ("`" ))
4629
4629
)
4630
4630
@@ -4636,7 +4636,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4636
4636
if isinstance (res , Series ) and res .name :
4637
4637
res .name = urllib .parse .unquote (res .name )
4638
4638
elif isinstance (res , DataFrame ):
4639
- res .columns = [ urllib .parse .unquote ( col ) for col in res .columns ]
4639
+ res .columns = map ( urllib .parse .unquote , res .columns )
4640
4640
else :
4641
4641
res = self .eval (expr , ** kwargs )
4642
4642
0 commit comments