Skip to content

Commit 291cd40

Browse files
committed
Simplify typing by being stricter
1 parent 1d30d70 commit 291cd40

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

pandas/core/computation/parsing.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def create_valid_python_identifier(name: str) -> str:
6363
return name
6464

6565

66-
def tokenize_and_clean_column_names(name: Union[str, int]) -> Union[str, int]:
66+
def clean_column_names(name: str) -> str:
6767
"""Function to emulate the cleaning of a backtick quoted name.
6868
6969
The purpose for this function is to see what happens to the name of
@@ -74,11 +74,8 @@ def tokenize_and_clean_column_names(name: Union[str, int]) -> Union[str, int]:
7474
Returns
7575
-------
7676
Returns the name after tokenizing and cleaning,
77-
or 'name' if that operation failed, or is a number.
77+
or 'name' if that operation failed.
7878
"""
79-
if not isinstance(name, str):
80-
return name
81-
8279
try:
8380
tokenized = tokenize_string(f"`{name}`")
8481
tokval = next(tokenized)[1]

pandas/core/generic.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@
9191
from pandas.io.formats.printing import pprint_thing
9292
from pandas.tseries.frequencies import to_offset
9393

94-
NameResolver = Dict[Union[str, int], ABCSeries]
95-
9694
# goal is to be able to define the docs close to function, while still being
9795
# able to share
9896
_shared_docs: Dict[str, str] = dict()
@@ -425,7 +423,7 @@ def _get_block_manager_axis(cls, axis):
425423
return m - axis
426424
return axis
427425

428-
def _get_axis_resolvers(self, axis: str) -> NameResolver:
426+
def _get_axis_resolvers(self, axis: str) -> Dict[str, ABCSeries]:
429427
# index or columns
430428
axis_index = getattr(self, axis)
431429
d = dict()
@@ -455,28 +453,28 @@ def _get_axis_resolvers(self, axis: str) -> NameResolver:
455453
d[axis] = dindex
456454
return d
457455

458-
def _get_index_resolvers(self) -> NameResolver:
459-
from pandas.core.computation.parsing import tokenize_and_clean_column_names
456+
def _get_index_resolvers(self) -> Dict[str, ABCSeries]:
457+
from pandas.core.computation.parsing import clean_column_names
460458

461-
d: NameResolver = {}
459+
d: Dict[str, ABCSeries] = {}
462460
for axis_name in self._AXIS_ORDERS:
463461
d.update(self._get_axis_resolvers(axis_name))
464462

465-
return {tokenize_and_clean_column_names(k): v for k, v in d.items()}
463+
return {clean_column_names(k): v for k, v in d.items() if k is not int}
466464

467-
def _get_cleaned_column_resolvers(self) -> NameResolver:
465+
def _get_cleaned_column_resolvers(self) -> Dict[str, ABCSeries]:
468466
"""Return the special character free column resolvers of a dataframe.
469467
470468
Column names with special characters are 'cleaned up' so that they can
471469
be referred to by backtick quoting.
472470
Used in :meth:`DataFrame.eval`.
473471
"""
474-
from pandas.core.computation.parsing import tokenize_and_clean_column_names
472+
from pandas.core.computation.parsing import clean_column_names
475473

476474
if isinstance(self, ABCSeries):
477-
return {tokenize_and_clean_column_names(self.name): self}
475+
return {clean_column_names(self.name): self}
478476

479-
return {tokenize_and_clean_column_names(k): v for k, v in self.items()}
477+
return {clean_column_names(k): v for k, v in self.items() if k is not int}
480478

481479
@property
482480
def _info_axis(self):

0 commit comments

Comments
 (0)