Skip to content

TYP/CLN: Optional[Hashable] -> pandas._typing.Label #32371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def style(self) -> "Styler":
"""

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

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

def iterrows(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
def iterrows(self) -> Iterable[Tuple[Label, Series]]:
"""
Iterate over DataFrame rows as (index, Series) pairs.

Expand Down Expand Up @@ -4043,7 +4043,7 @@ def set_index(
"one-dimensional arrays."
)

missing: List[Optional[Hashable]] = []
missing: List[Label] = []
for col in keys:
if isinstance(
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
Expand Down Expand Up @@ -4082,7 +4082,7 @@ def set_index(
else:
arrays.append(self.index)

to_remove: List[Optional[Hashable]] = []
to_remove: List[Label] = []
for col in keys:
if isinstance(col, ABCMultiIndex):
for n in range(col.nlevels):
Expand Down Expand Up @@ -4137,7 +4137,7 @@ def reset_index(
drop: bool = False,
inplace: bool = False,
col_level: Hashable = 0,
col_fill: Optional[Hashable] = "",
col_fill: Label = "",
) -> Optional["DataFrame"]:
"""
Reset the index, or a level of it.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9729,7 +9729,7 @@ def describe_1d(data):

ldesc = [describe_1d(s) for _, s in data.items()]
# set a convenient order for rows
names: List[Optional[Hashable]] = []
names: List[Label] = []
ldesc_indexes = sorted((x.index for x in ldesc), key=len)
for idxnames in ldesc_indexes:
for name in idxnames:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import TYPE_CHECKING, Any, FrozenSet, Hashable, Optional, Union
from typing import TYPE_CHECKING, Any, FrozenSet, Hashable, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -5546,7 +5546,7 @@ def default_index(n):
return RangeIndex(0, n, name=None)


def maybe_extract_name(name, obj, cls) -> Optional[Hashable]:
def maybe_extract_name(name, obj, cls) -> Label:
"""
If no name is passed, then extract it from data, validating hashability.
"""
Expand Down
16 changes: 6 additions & 10 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Concat routines.
"""

from typing import Hashable, Iterable, List, Mapping, Optional, Union, overload
from typing import Iterable, List, Mapping, Union, overload

import numpy as np

from pandas._typing import FrameOrSeriesUnion
from pandas._typing import FrameOrSeriesUnion, Label

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

Expand All @@ -32,7 +32,7 @@

@overload
def concat(
objs: Union[Iterable["DataFrame"], Mapping[Optional[Hashable], "DataFrame"]],
objs: Union[Iterable["DataFrame"], Mapping[Label, "DataFrame"]],
axis=0,
join: str = "outer",
ignore_index: bool = False,
Expand All @@ -48,9 +48,7 @@ def concat(

@overload
def concat(
objs: Union[
Iterable[FrameOrSeriesUnion], Mapping[Optional[Hashable], FrameOrSeriesUnion]
],
objs: Union[Iterable[FrameOrSeriesUnion], Mapping[Label, FrameOrSeriesUnion]],
axis=0,
join: str = "outer",
ignore_index: bool = False,
Expand All @@ -65,9 +63,7 @@ def concat(


def concat(
objs: Union[
Iterable[FrameOrSeriesUnion], Mapping[Optional[Hashable], FrameOrSeriesUnion]
],
objs: Union[Iterable[FrameOrSeriesUnion], Mapping[Label, FrameOrSeriesUnion]],
axis=0,
join="outer",
ignore_index: bool = False,
Expand Down Expand Up @@ -536,7 +532,7 @@ def _get_concat_axis(self) -> Index:
idx = ibase.default_index(len(self.objs))
return idx
elif self.keys is None:
names: List[Optional[Hashable]] = [None] * len(self.objs)
names: List[Label] = [None] * len(self.objs)
num = 0
has_names = False
for i, x in enumerate(self.objs):
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,7 @@ def __array_ufunc__(
inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
result = getattr(ufunc, method)(*inputs, **kwargs)

name: Label
if len(set(names)) == 1:
name = names[0]
else:
name = None
name = names[0] if len(set(names)) == 1 else None

def construct_return(result):
if lib.is_scalar(result):
Expand Down
18 changes: 4 additions & 14 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@
import itertools
import os
import re
from typing import (
TYPE_CHECKING,
Any,
Dict,
Hashable,
List,
Optional,
Tuple,
Type,
Union,
)
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
import warnings

import numpy as np
Expand All @@ -27,7 +17,7 @@

from pandas._libs import lib, writers as libwriters
from pandas._libs.tslibs import timezones
from pandas._typing import ArrayLike, FrameOrSeries
from pandas._typing import ArrayLike, FrameOrSeries, Label
from pandas.compat._optional import import_optional_dependency
from pandas.errors import PerformanceWarning
from pandas.util._decorators import cache_readonly
Expand Down Expand Up @@ -2811,7 +2801,7 @@ def read_multi_index(

levels = []
codes = []
names: List[Optional[Hashable]] = []
names: List[Label] = []
for i in range(nlevels):
level_key = f"{key}_level{i}"
node = getattr(self.group, level_key)
Expand Down Expand Up @@ -2976,7 +2966,7 @@ class SeriesFixed(GenericFixed):
pandas_kind = "series"
attributes = ["name"]

name: Optional[Hashable]
name: Label

@property
def shape(self):
Expand Down