Skip to content

TYPING : Series.name -> Optional[Hashable] #29164

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 2 commits into from
Oct 22, 2019
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
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
):

Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
FrozenSet,
Hashable,
Iterable,
Optional,
Sequence,
Tuple,
Type,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note there is a similar definition for SeriesGroupBy - not sure if that needs to change as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe IndexLabel = Optional[Hashable] in _typing.py

obj = self._selected_obj
if self.axis == 1:
obj = obj.T
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down