diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f2074bab276ac..a4d3e7058d7de 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -871,7 +871,7 @@ def style(self): """ @Appender(_shared_docs["items"]) - def items(self) -> Iterable[Tuple[Hashable, Series]]: + def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]: if self.columns.is_unique and hasattr(self, "_item_cache"): for k in self.columns: yield k, self._get_item_cache(k) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4693908e15f60..a3cc7c9209bb4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -194,7 +194,7 @@ class NDFrame(PandasObject, SelectionMixin): # TODO(PY36): replace with _attrs : Dict[Hashable, Any] # We need the TYPE_CHECKING, because _attrs is not a class attribute # and Py35 doesn't support the new syntax. - _attrs = {} # type: Dict[Hashable, Any] + _attrs = {} # type: Dict[Optional[Hashable], Any] # ---------------------------------------------------------------------- # Constructors @@ -205,7 +205,7 @@ def __init__( axes: Optional[List[Index]] = None, copy: bool = False, dtype: Optional[Dtype] = None, - attrs: Optional[Mapping[Hashable, Any]] = None, + attrs: Optional[Mapping[Optional[Hashable], Any]] = None, fastpath: bool = False, ): @@ -248,7 +248,7 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): # ---------------------------------------------------------------------- @property - def attrs(self) -> Dict[Hashable, Any]: + def attrs(self) -> Dict[Optional[Hashable], Any]: """ Dictionary of global attributes on this object. """ @@ -257,7 +257,7 @@ def attrs(self) -> Dict[Hashable, Any]: return self._attrs @attrs.setter - def attrs(self, value: Mapping[Hashable, Any]) -> None: + def attrs(self, value: Mapping[Optional[Hashable], Any]) -> None: self._attrs = dict(value) @property @@ -3149,10 +3149,10 @@ def to_csv( sep: str = ",", na_rep: str = "", float_format: Optional[str] = None, - columns: Optional[Sequence[Hashable]] = None, + columns: Optional[Sequence[Optional[Hashable]]] = None, header: Union[bool_t, List[str]] = True, index: bool_t = True, - index_label: Optional[Union[bool_t, str, Sequence[Hashable]]] = None, + index_label: Optional[Union[bool_t, str, Sequence[Optional[Hashable]]]] = None, mode: str = "w", encoding: Optional[str] = None, compression: Optional[Union[str, Dict[str, str]]] = "infer", diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 5c7c56e2a31df..8ba9dbcc575f7 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -17,6 +17,7 @@ FrozenSet, Hashable, Iterable, + Optional, Sequence, Tuple, Type, @@ -142,7 +143,7 @@ def pinner(cls): class SeriesGroupBy(GroupBy): _apply_whitelist = base.series_apply_whitelist - def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]: + def _iterate_slices(self) -> Iterable[Tuple[Optional[Hashable], Series]]: yield self._selection_name, self._selected_obj @property @@ -926,7 +927,7 @@ def aggregate(self, func=None, *args, **kwargs): agg = aggregate - def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]: + def _iterate_slices(self) -> Iterable[Tuple[Optional[Hashable], Series]]: obj = self._selected_obj if self.axis == 1: obj = obj.T diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index f88f2e21bd595..7d1c74e415658 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -745,7 +745,7 @@ def _python_apply_general(self, f): keys, values, not_indexed_same=mutated or self.mutated ) - def _iterate_slices(self) -> Iterable[Tuple[Hashable, Series]]: + def _iterate_slices(self) -> Iterable[Tuple[Optional[Hashable], Series]]: raise AbstractMethodError(self) def transform(self, func, *args, **kwargs): diff --git a/pandas/core/series.py b/pandas/core/series.py index 5f1a7624f47e4..3e9d3d5c04559 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5,7 +5,7 @@ from io import StringIO from shutil import get_terminal_size from textwrap import dedent -from typing import Any, Callable, Hashable, List +from typing import Any, Callable, Hashable, List, Optional import warnings import numpy as np @@ -472,11 +472,11 @@ def dtypes(self): return self._data.dtype @property - def name(self) -> Hashable: + def name(self) -> Optional[Hashable]: return self.attrs.get("name", None) @name.setter - def name(self, value: Hashable) -> None: + def name(self, value: Optional[Hashable]) -> None: if not is_hashable(value): raise TypeError("Series.name must be a hashable type") self.attrs["name"] = value