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