Skip to content

TYP: Add type hint for BaseGrouper in groupby._Groupby #29675

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
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
6 changes: 3 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class providing the base-class of operations.
from pandas.core.construction import extract_array
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame
from pandas.core.groupby import base
from pandas.core.groupby import base, ops
from pandas.core.index import CategoricalIndex, Index, MultiIndex
from pandas.core.series import Series
from pandas.core.sorting import get_group_index_sorter
Expand Down Expand Up @@ -345,7 +345,7 @@ def __init__(
keys=None,
axis: int = 0,
level=None,
grouper=None,
grouper: "Optional[ops.BaseGrouper]" = None,
exclusions=None,
selection=None,
as_index: bool = True,
Expand Down Expand Up @@ -2480,7 +2480,7 @@ def get_groupby(
by=None,
axis: int = 0,
level=None,
grouper=None,
grouper: "Optional[ops.BaseGrouper]" = None,
exclusions=None,
selection=None,
as_index: bool = True,
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from pandas.core.arrays import Categorical, ExtensionArray
import pandas.core.common as com
from pandas.core.frame import DataFrame
from pandas.core.groupby import ops
from pandas.core.groupby.categorical import recode_for_groupby, recode_from_groupby
from pandas.core.groupby.ops import BaseGrouper
from pandas.core.index import CategoricalIndex, Index, MultiIndex
from pandas.core.series import Series

Expand Down Expand Up @@ -392,7 +392,7 @@ def ngroups(self) -> int:
@cache_readonly
def indices(self):
# we have a list of groupers
if isinstance(self.grouper, BaseGrouper):
if isinstance(self.grouper, ops.BaseGrouper):
return self.grouper.indices

values = ensure_categorical(self.grouper)
Expand All @@ -419,7 +419,7 @@ def group_index(self) -> Index:
def _make_codes(self) -> None:
if self._codes is None or self._group_index is None:
# we have a list of groupers
if isinstance(self.grouper, BaseGrouper):
if isinstance(self.grouper, ops.BaseGrouper):
codes = self.grouper.codes_info
uniques = self.grouper.result_index
else:
Expand All @@ -442,7 +442,7 @@ def get_grouper(
observed: bool = False,
mutated: bool = False,
validate: bool = True,
) -> Tuple[BaseGrouper, List[Hashable], FrameOrSeries]:
) -> "Tuple[ops.BaseGrouper, List[Hashable], FrameOrSeries]":
Copy link
Member

Choose a reason for hiding this comment

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

any reason to add quotes? don't we lose the black formatting checking by doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the quotes we get a circular import error, so needs to be quoted.

"""
Create and return a BaseGrouper, which is an internal
mapping of how to create the grouper indexers.
Expand Down Expand Up @@ -524,7 +524,7 @@ def get_grouper(
return grouper, [key.key], obj

# already have a BaseGrouper, just return it
elif isinstance(key, BaseGrouper):
elif isinstance(key, ops.BaseGrouper):
return key, [], obj

# In the future, a tuple key will always mean an actual key,
Expand Down Expand Up @@ -671,7 +671,7 @@ def is_in_obj(gpr) -> bool:
groupings.append(Grouping(Index([], dtype="int"), np.array([], dtype=np.intp)))

# create the internals grouper
grouper = BaseGrouper(group_axis, groupings, sort=sort, mutated=mutated)
grouper = ops.BaseGrouper(group_axis, groupings, sort=sort, mutated=mutated)
return grouper, exclusions, obj


Expand Down