Skip to content

Commit 8914c01

Browse files
TYP/CLN: Optional[Hashable] -> pandas._typing.Label (#32371)
1 parent f7bed05 commit 8914c01

File tree

6 files changed

+20
-38
lines changed

6 files changed

+20
-38
lines changed

pandas/core/frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def style(self) -> "Styler":
890890
"""
891891

892892
@Appender(_shared_docs["items"])
893-
def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
893+
def items(self) -> Iterable[Tuple[Label, Series]]:
894894
if self.columns.is_unique and hasattr(self, "_item_cache"):
895895
for k in self.columns:
896896
yield k, self._get_item_cache(k)
@@ -899,10 +899,10 @@ def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
899899
yield k, self._ixs(i, axis=1)
900900

901901
@Appender(_shared_docs["items"])
902-
def iteritems(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
902+
def iteritems(self) -> Iterable[Tuple[Label, Series]]:
903903
yield from self.items()
904904

905-
def iterrows(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
905+
def iterrows(self) -> Iterable[Tuple[Label, Series]]:
906906
"""
907907
Iterate over DataFrame rows as (index, Series) pairs.
908908
@@ -4043,7 +4043,7 @@ def set_index(
40434043
"one-dimensional arrays."
40444044
)
40454045

4046-
missing: List[Optional[Hashable]] = []
4046+
missing: List[Label] = []
40474047
for col in keys:
40484048
if isinstance(
40494049
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
@@ -4082,7 +4082,7 @@ def set_index(
40824082
else:
40834083
arrays.append(self.index)
40844084

4085-
to_remove: List[Optional[Hashable]] = []
4085+
to_remove: List[Label] = []
40864086
for col in keys:
40874087
if isinstance(col, ABCMultiIndex):
40884088
for n in range(col.nlevels):
@@ -4137,7 +4137,7 @@ def reset_index(
41374137
drop: bool = False,
41384138
inplace: bool = False,
41394139
col_level: Hashable = 0,
4140-
col_fill: Optional[Hashable] = "",
4140+
col_fill: Label = "",
41414141
) -> Optional["DataFrame"]:
41424142
"""
41434143
Reset the index, or a level of it.

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9729,7 +9729,7 @@ def describe_1d(data):
97299729

97309730
ldesc = [describe_1d(s) for _, s in data.items()]
97319731
# set a convenient order for rows
9732-
names: List[Optional[Hashable]] = []
9732+
names: List[Label] = []
97339733
ldesc_indexes = sorted((x.index for x in ldesc), key=len)
97349734
for idxnames in ldesc_indexes:
97359735
for name in idxnames:

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
import operator
33
from textwrap import dedent
4-
from typing import TYPE_CHECKING, Any, FrozenSet, Hashable, Optional, Union
4+
from typing import TYPE_CHECKING, Any, FrozenSet, Hashable, Union
55
import warnings
66

77
import numpy as np
@@ -5546,7 +5546,7 @@ def default_index(n):
55465546
return RangeIndex(0, n, name=None)
55475547

55485548

5549-
def maybe_extract_name(name, obj, cls) -> Optional[Hashable]:
5549+
def maybe_extract_name(name, obj, cls) -> Label:
55505550
"""
55515551
If no name is passed, then extract it from data, validating hashability.
55525552
"""

pandas/core/reshape/concat.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Concat routines.
33
"""
44

5-
from typing import Hashable, Iterable, List, Mapping, Optional, Union, overload
5+
from typing import Iterable, List, Mapping, Union, overload
66

77
import numpy as np
88

9-
from pandas._typing import FrameOrSeriesUnion
9+
from pandas._typing import FrameOrSeriesUnion, Label
1010

1111
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
1212

@@ -32,7 +32,7 @@
3232

3333
@overload
3434
def concat(
35-
objs: Union[Iterable["DataFrame"], Mapping[Optional[Hashable], "DataFrame"]],
35+
objs: Union[Iterable["DataFrame"], Mapping[Label, "DataFrame"]],
3636
axis=0,
3737
join: str = "outer",
3838
ignore_index: bool = False,
@@ -48,9 +48,7 @@ def concat(
4848

4949
@overload
5050
def concat(
51-
objs: Union[
52-
Iterable[FrameOrSeriesUnion], Mapping[Optional[Hashable], FrameOrSeriesUnion]
53-
],
51+
objs: Union[Iterable[FrameOrSeriesUnion], Mapping[Label, FrameOrSeriesUnion]],
5452
axis=0,
5553
join: str = "outer",
5654
ignore_index: bool = False,
@@ -65,9 +63,7 @@ def concat(
6563

6664

6765
def concat(
68-
objs: Union[
69-
Iterable[FrameOrSeriesUnion], Mapping[Optional[Hashable], FrameOrSeriesUnion]
70-
],
66+
objs: Union[Iterable[FrameOrSeriesUnion], Mapping[Label, FrameOrSeriesUnion]],
7167
axis=0,
7268
join="outer",
7369
ignore_index: bool = False,
@@ -536,7 +532,7 @@ def _get_concat_axis(self) -> Index:
536532
idx = ibase.default_index(len(self.objs))
537533
return idx
538534
elif self.keys is None:
539-
names: List[Optional[Hashable]] = [None] * len(self.objs)
535+
names: List[Label] = [None] * len(self.objs)
540536
num = 0
541537
has_names = False
542538
for i, x in enumerate(self.objs):

pandas/core/series.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,7 @@ def __array_ufunc__(
692692
inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
693693
result = getattr(ufunc, method)(*inputs, **kwargs)
694694

695-
name: Label
696-
if len(set(names)) == 1:
697-
name = names[0]
698-
else:
699-
name = None
695+
name = names[0] if len(set(names)) == 1 else None
700696

701697
def construct_return(result):
702698
if lib.is_scalar(result):

pandas/io/pytables.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@
88
import itertools
99
import os
1010
import re
11-
from typing import (
12-
TYPE_CHECKING,
13-
Any,
14-
Dict,
15-
Hashable,
16-
List,
17-
Optional,
18-
Tuple,
19-
Type,
20-
Union,
21-
)
11+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
2212
import warnings
2313

2414
import numpy as np
@@ -27,7 +17,7 @@
2717

2818
from pandas._libs import lib, writers as libwriters
2919
from pandas._libs.tslibs import timezones
30-
from pandas._typing import ArrayLike, FrameOrSeries
20+
from pandas._typing import ArrayLike, FrameOrSeries, Label
3121
from pandas.compat._optional import import_optional_dependency
3222
from pandas.errors import PerformanceWarning
3323
from pandas.util._decorators import cache_readonly
@@ -2811,7 +2801,7 @@ def read_multi_index(
28112801

28122802
levels = []
28132803
codes = []
2814-
names: List[Optional[Hashable]] = []
2804+
names: List[Label] = []
28152805
for i in range(nlevels):
28162806
level_key = f"{key}_level{i}"
28172807
node = getattr(self.group, level_key)
@@ -2976,7 +2966,7 @@ class SeriesFixed(GenericFixed):
29762966
pandas_kind = "series"
29772967
attributes = ["name"]
29782968

2979-
name: Optional[Hashable]
2969+
name: Label
29802970

29812971
@property
29822972
def shape(self):

0 commit comments

Comments
 (0)