From 8d08fd510df3ec8c52045bb8f6c6e0279fa9bc1e Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 25 Sep 2020 14:41:40 -0400 Subject: [PATCH 1/3] TYP: selection and groups type-hinting in groupby --- pandas/core/base.py | 3 ++- pandas/core/groupby/groupby.py | 20 ++++++++++++++------ pandas/core/groupby/grouper.py | 6 +++--- pandas/core/groupby/ops.py | 6 ++++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 4d5cddc086b2a..fc0a4bcdcdbea 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -9,6 +9,7 @@ import numpy as np import pandas._libs.lib as lib +from pandas._typing import IndexLabel from pandas.compat import PYPY from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -135,7 +136,7 @@ class SelectionMixin: object sub-classes need to define: obj, exclusions """ - _selection = None + _selection: Optional[IndexLabel] = None _internal_names = ["_cache", "__setstate__"] _internal_names_set = set(_internal_names) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index f1a61f433fc51..0f6aa904972dc 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -37,7 +37,14 @@ class providing the base-class of operations. from pandas._libs import Timestamp, lib import pandas._libs.groupby as libgroupby -from pandas._typing import F, FrameOrSeries, FrameOrSeriesUnion, Label, Scalar +from pandas._typing import ( + F, + FrameOrSeries, + FrameOrSeriesUnion, + IndexLabel, + Label, + Scalar, +) from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError from pandas.util._decorators import Appender, Substitution, cache_readonly, doc @@ -68,6 +75,8 @@ class providing the base-class of operations. from pandas.core.sorting import get_group_index_sorter from pandas.core.util.numba_ import NUMBA_FUNC_CACHE +from pandas.io.formats.printing import PrettyDict + _common_see_also = """ See Also -------- @@ -487,10 +496,10 @@ def __init__( obj: FrameOrSeries, keys: Optional[_KeysArgType] = None, axis: int = 0, - level=None, + level: Optional[IndexLabel] = None, grouper: Optional["ops.BaseGrouper"] = None, exclusions: Optional[Set[Label]] = None, - selection=None, + selection: Optional[IndexLabel] = None, as_index: bool = True, sort: bool = True, group_keys: bool = True, @@ -499,7 +508,6 @@ def __init__( mutated: bool = False, dropna: bool = True, ): - self._selection = selection assert isinstance(obj, NDFrame), type(obj) @@ -547,7 +555,7 @@ def __repr__(self) -> str: # TODO: Better repr for GroupBy object return object.__repr__(self) - def _assure_grouper(self): + def _assure_grouper(self) -> None: """ We create the grouper on instantiation sub-classes may have a different policy. @@ -555,7 +563,7 @@ def _assure_grouper(self): pass @property - def groups(self): + def groups(self) -> PrettyDict[Hashable, np.ndarray]: """ Dict {group name -> group labels}. """ diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 6263d5337f42f..acdf29787f3c5 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -2,7 +2,7 @@ Provide user facing operators for doing the split part of the split-apply-combine paradigm. """ -from typing import Dict, Hashable, List, Optional, Set, Tuple +from typing import Hashable, List, Optional, Set, Tuple import warnings import numpy as np @@ -29,7 +29,7 @@ from pandas.core.indexes.api import CategoricalIndex, Index, MultiIndex from pandas.core.series import Series -from pandas.io.formats.printing import pprint_thing +from pandas.io.formats.printing import PrettyDict, pprint_thing class Grouper: @@ -600,7 +600,7 @@ def _make_codes(self) -> None: self._group_index = uniques @cache_readonly - def groups(self) -> Dict[Hashable, np.ndarray]: + def groups(self) -> PrettyDict[Hashable, np.ndarray]: return self.index.groupby(Categorical.from_codes(self.codes, self.group_index)) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index b3f91d4623c84..84fe5d341cf45 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -7,7 +7,7 @@ """ import collections -from typing import List, Optional, Sequence, Tuple, Type +from typing import Hashable, List, Optional, Sequence, Tuple, Type import numpy as np @@ -56,6 +56,8 @@ get_indexer_dict, ) +from pandas.io.formats.printing import PrettyDict + class BaseGrouper: """ @@ -246,7 +248,7 @@ def size(self) -> Series: return Series(out, index=self.result_index, dtype="int64") @cache_readonly - def groups(self): + def groups(self) -> PrettyDict[Hashable, np.ndarray]: """ dict {group name -> group labels} """ if len(self.groupings) == 1: return self.groupings[0].groups From a50d05979d6820071e3fdb32833d3b11427f478d Mon Sep 17 00:00:00 2001 From: rhshadrach Date: Tue, 29 Sep 2020 20:59:31 -0400 Subject: [PATCH 2/3] PrettyDict -> Dict --- pandas/core/groupby/groupby.py | 5 ++--- pandas/core/groupby/grouper.py | 6 +++--- pandas/core/groupby/ops.py | 6 ++---- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 0f6aa904972dc..89d9a6d6817e8 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -75,8 +75,6 @@ class providing the base-class of operations. from pandas.core.sorting import get_group_index_sorter from pandas.core.util.numba_ import NUMBA_FUNC_CACHE -from pandas.io.formats.printing import PrettyDict - _common_see_also = """ See Also -------- @@ -508,6 +506,7 @@ def __init__( mutated: bool = False, dropna: bool = True, ): + self._selection = selection assert isinstance(obj, NDFrame), type(obj) @@ -563,7 +562,7 @@ def _assure_grouper(self) -> None: pass @property - def groups(self) -> PrettyDict[Hashable, np.ndarray]: + def groups(self) -> Dict[Hashable, np.ndarray]: """ Dict {group name -> group labels}. """ diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index a7f6e080a8b07..a509acb3604e1 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -2,7 +2,7 @@ Provide user facing operators for doing the split part of the split-apply-combine paradigm. """ -from typing import Hashable, List, Optional, Set, Tuple +from typing import Dict, Hashable, List, Optional, Set, Tuple import warnings import numpy as np @@ -29,7 +29,7 @@ from pandas.core.indexes.api import CategoricalIndex, Index, MultiIndex from pandas.core.series import Series -from pandas.io.formats.printing import PrettyDict, pprint_thing +from pandas.io.formats.printing import pprint_thing class Grouper: @@ -607,7 +607,7 @@ def _make_codes(self) -> None: self._group_index = uniques @cache_readonly - def groups(self) -> PrettyDict[Hashable, np.ndarray]: + def groups(self) -> Dict[Hashable, np.ndarray]: return self.index.groupby(Categorical.from_codes(self.codes, self.group_index)) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 8df81dcb5dedf..6de2bfab1ea82 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -7,7 +7,7 @@ """ import collections -from typing import Hashable, List, Optional, Sequence, Tuple, Type +from typing import Dict, Hashable, List, Optional, Sequence, Tuple, Type import numpy as np @@ -56,8 +56,6 @@ get_indexer_dict, ) -from pandas.io.formats.printing import PrettyDict - class BaseGrouper: """ @@ -250,7 +248,7 @@ def size(self) -> Series: return Series(out, index=self.result_index, dtype="int64") @cache_readonly - def groups(self) -> PrettyDict[Hashable, np.ndarray]: + def groups(self) -> Dict[Hashable, np.ndarray]: """ dict {group name -> group labels} """ if len(self.groupings) == 1: return self.groupings[0].groups From a2f99c34f650fbf6904c41d1965be7170cee74b8 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 4 Oct 2020 10:58:47 -0400 Subject: [PATCH 3/3] Cleanup from merge --- pandas/core/base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index d05a5dc9e9e8f..564a0af3527c5 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -9,8 +9,7 @@ import numpy as np import pandas._libs.lib as lib -from pandas._typing import AggFuncType, AggFuncTypeBase, Label -from pandas._typing import IndexLabel +from pandas._typing import AggFuncType, AggFuncTypeBase, IndexLabel, Label from pandas.compat import PYPY from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError