@@ -4546,8 +4546,9 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4546
4546
For other characters that fall outside the ASCII range (U+0001..U+007F)
4547
4547
and those that are not further specified in PEP 3131,
4548
4548
the query parser will raise an error.
4549
- This excludes whitespace different than the space character
4550
- and the backtick itself (backtick cannot be escaped).
4549
+ This excludes whitespace different than the space character,
4550
+ but also the hashtag (as it is used for comments) and the backtick
4551
+ itself (backtick can also not be escaped).
4551
4552
4552
4553
See also the `Python documentation about lexical analysis
4553
4554
<https://docs.python.org/3/reference/lexical_analysis.html>`__
@@ -4606,16 +4607,15 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4606
4607
if any (("#" in col ) or ("'" in col ) or ('"' in col ) for col in self .columns ):
4607
4608
# Create a copy of `self` with column names escaped
4608
4609
escaped_self = self .copy ()
4609
- escaped_self .columns = [
4610
- urllib .parse .quote (col ) for col in escaped_self .columns
4611
- ]
4610
+ escaped_self .columns = map (urllib .parse .quote , escaped_self .columns )
4612
4611
4613
4612
# In expr, escape column names between backticks
4614
- column_name_to_escaped_name = {
4613
+ column_name_to_escaped = {
4615
4614
col : urllib .parse .quote (col ) for col in self .columns
4616
4615
}
4616
+ # Odd-number indexes are column names
4617
4617
escaped_expr = "`" .join (
4618
- (column_name_to_escaped_name .get (token , token ) if (i % 2 ) else token )
4618
+ (column_name_to_escaped .get (token , token ) if (i % 2 ) else token )
4619
4619
for i , token in enumerate (expr .split ("`" ))
4620
4620
)
4621
4621
@@ -4627,7 +4627,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4627
4627
if isinstance (res , Series ) and res .name :
4628
4628
res .name = urllib .parse .unquote (res .name )
4629
4629
elif isinstance (res , DataFrame ):
4630
- res .columns = [ urllib .parse .unquote ( col ) for col in res .columns ]
4630
+ res .columns = map ( urllib .parse .unquote , res .columns )
4631
4631
else :
4632
4632
res = self .eval (expr , ** kwargs )
4633
4633
0 commit comments