Skip to content

Commit ad0c5db

Browse files
committed
Add function annotations and explained edge case in documentation
1 parent 9ad2378 commit ad0c5db

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

pandas/core/computation/expr.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import itertools as it
88
import token
99
import tokenize
10-
from typing import Type
10+
from typing import Any, Generator, Tuple, Type
1111

1212
import numpy as np
1313

@@ -39,8 +39,10 @@
3939

4040
import pandas.io.formats.printing as printing
4141

42+
Tok = Tuple[int, str]
4243

43-
def tokenize_backtick_quoted_string(token_generator):
44+
45+
def tokenize_backtick_quoted_string(token_generator) -> Tok:
4446
"""Creates a token from a backtick quoted string.
4547
Moves the token_generator forwards till right after the next backtick."""
4648
prev_toknum = token.OP # This will trigger the first token to have no space
@@ -58,7 +60,7 @@ def tokenize_backtick_quoted_string(token_generator):
5860
return _BACKTICK_QUOTED_STRING, new_tokval
5961

6062

61-
def tokenize_string(source):
63+
def tokenize_string(source: str) -> Generator[Tok, None, None]:
6264
"""
6365
Tokenize a Python source code string.
6466

pandas/core/frame.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -3142,12 +3142,17 @@ def query(self, expr, inplace=False, **kwargs):
31423142
names that start with a digit, or are a Python keyword. Basically
31433143
when it is not valid Python identifier.
31443144
3145-
NB. You cannot use this if there are multiple invalid characters
3146-
next to each other, like `very*=invalid`.
3147-
31483145
For example, if one of your columns is called ``a a`` and you want
31493146
to sum it with ``b``, your query should be ```a a` + b``.
31503147
3148+
NB. A combination of an invalid character with a space, or
3149+
multiple spaces will collapse the space. The reason is that the
3150+
string will be parsed as Python code
3151+
and in Python ``a*b`` and ``a* b`` are equal.
3152+
So, if you want to refer to a column called ``"a* b"`` you
3153+
can use ```a*b```, but it is recommend in this case to change the
3154+
column name or use bracket notation, to prevent very opaque code.
3155+
31513156
inplace : bool
31523157
Whether the query should modify the data in place or return
31533158
a modified copy.

0 commit comments

Comments
 (0)