From 7213d2ae6cac3546d3d39d30522a6c54aad9343e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 20 Feb 2020 17:11:02 +0000 Subject: [PATCH 1/2] TYP/CLN: Optional[Hashable] -> pandas._typing.Label --- pandas/core/frame.py | 12 ++++++------ pandas/core/generic.py | 11 +++++------ pandas/core/indexes/base.py | 4 ++-- pandas/core/reshape/concat.py | 16 ++++++---------- pandas/core/series.py | 13 ++++--------- pandas/io/pytables.py | 18 ++++-------------- 6 files changed, 27 insertions(+), 47 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9fe1ec7b792c8..2fd9052f1d736 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -893,7 +893,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) @@ -902,10 +902,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. @@ -4046,7 +4046,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) @@ -4083,7 +4083,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): @@ -4138,7 +4138,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. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 579daae2b15c6..9706233766ff1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -13,7 +13,6 @@ Callable, Dict, FrozenSet, - Hashable, List, Mapping, Optional, @@ -191,7 +190,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin): _metadata: List[str] = [] _is_copy = None _data: BlockManager - _attrs: Dict[Optional[Hashable], Any] + _attrs: Dict[Label, Any] _typ: str # ---------------------------------------------------------------------- @@ -203,7 +202,7 @@ def __init__( axes: Optional[List[Index]] = None, copy: bool = False, dtype: Optional[Dtype] = None, - attrs: Optional[Mapping[Optional[Hashable], Any]] = None, + attrs: Optional[Mapping[Label, Any]] = None, fastpath: bool = False, ): @@ -246,7 +245,7 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): # ---------------------------------------------------------------------- @property - def attrs(self) -> Dict[Optional[Hashable], Any]: + def attrs(self) -> Dict[Label, Any]: """ Dictionary of global attributes on this object. @@ -259,7 +258,7 @@ def attrs(self) -> Dict[Optional[Hashable], Any]: return self._attrs @attrs.setter - def attrs(self, value: Mapping[Optional[Hashable], Any]) -> None: + def attrs(self, value: Mapping[Label, Any]) -> None: self._attrs = dict(value) def _validate_dtype(self, dtype): @@ -9726,7 +9725,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: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 14ee21ea5614c..046a8920ed290 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 @@ -5583,7 +5583,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. """ diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index d9f21f0b274ac..246e5b3bbf2e0 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -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 @@ -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, @@ -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, @@ -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, @@ -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): diff --git a/pandas/core/series.py b/pandas/core/series.py index 9c0ff9780da3e..98716b4ecf437 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -9,7 +9,6 @@ TYPE_CHECKING, Any, Callable, - Hashable, Iterable, List, Optional, @@ -177,7 +176,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame): _typ = "series" - _name: Optional[Hashable] + _name: Label _metadata: List[str] = ["name"] _internal_names_set = {"index"} | generic.NDFrame._internal_names_set _accessors = {"dt", "cat", "str", "sparse"} @@ -435,11 +434,11 @@ def dtypes(self): return self._data.dtype @property - def name(self) -> Optional[Hashable]: + def name(self) -> Label: return self._name @name.setter - def name(self, value: Optional[Hashable]) -> None: + def name(self, value: Label) -> None: if not is_hashable(value): raise TypeError("Series.name must be a hashable type") object.__setattr__(self, "_name", value) @@ -689,11 +688,7 @@ def __array_ufunc__( inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs) result = getattr(ufunc, method)(*inputs, **kwargs) - name: Optional[Hashable] - 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): diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 048aa8b1915d1..39a5e75de173d 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -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 @@ -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 @@ -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) @@ -2976,7 +2966,7 @@ class SeriesFixed(GenericFixed): pandas_kind = "series" attributes = ["name"] - name: Optional[Hashable] + name: Label @property def shape(self): From a599d17c4081cfa5d20303b689b347d3be4feeb9 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 5 Mar 2020 19:04:42 +0000 Subject: [PATCH 2/2] revert change for attrs --- pandas/core/generic.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4b68eaf008d82..866b58c1ffe3d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -13,6 +13,7 @@ Callable, Dict, FrozenSet, + Hashable, List, Mapping, Optional, @@ -189,7 +190,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin): _metadata: List[str] = [] _is_copy = None _data: BlockManager - _attrs: Dict[Label, Any] + _attrs: Dict[Optional[Hashable], Any] _typ: str # ---------------------------------------------------------------------- @@ -199,7 +200,7 @@ def __init__( self, data: BlockManager, copy: bool = False, - attrs: Optional[Mapping[Label, Any]] = None, + attrs: Optional[Mapping[Optional[Hashable], Any]] = None, ): # copy kwarg is retained for mypy compat, is not used @@ -233,7 +234,7 @@ def _init_mgr(cls, mgr, axes=None, dtype=None, copy=False): # ---------------------------------------------------------------------- @property - def attrs(self) -> Dict[Label, Any]: + def attrs(self) -> Dict[Optional[Hashable], Any]: """ Dictionary of global attributes on this object. @@ -246,7 +247,7 @@ def attrs(self) -> Dict[Label, Any]: return self._attrs @attrs.setter - def attrs(self, value: Mapping[Label, Any]) -> None: + def attrs(self, value: Mapping[Optional[Hashable], Any]) -> None: self._attrs = dict(value) @classmethod