Skip to content

TYP: implement typing.Manager2D #40853

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
Apr 13, 2021
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
3 changes: 3 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
# internals
Manager = Union["ArrayManager", "BlockManager", "SingleBlockManager"]
SingleManager = Union["SingleArrayManager", "SingleBlockManager"]
Manager2D = Union["ArrayManager", "BlockManager"]
# TODO: Manager2d excludes SingleBlockManager, but does not exclude
# SingleArrayManager

# indexing
# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
Expand Down
16 changes: 11 additions & 5 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ArrayLike,
FrameOrSeries,
FrameOrSeriesUnion,
Manager,
Manager2D,
)
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -177,6 +177,9 @@ def pinner(cls):
class SeriesGroupBy(GroupBy[Series]):
_apply_allowlist = base.series_apply_allowlist

# Defined as a cache_readonly in SelectionMixin
_obj_with_exclusions: Series

def _iterate_slices(self) -> Iterable[Series]:
yield self._selected_obj

Expand Down Expand Up @@ -927,6 +930,9 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None):
@pin_allowlisted_properties(DataFrame, base.dataframe_apply_allowlist)
class DataFrameGroupBy(GroupBy[DataFrame]):

# Defined as a cache_readonly in SelectionMixin
_obj_with_exclusions: DataFrame

_apply_allowlist = base.dataframe_apply_allowlist

_agg_examples_doc = dedent(
Expand Down Expand Up @@ -1095,9 +1101,9 @@ def _cython_agg_general(

def _cython_agg_manager(
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
) -> Manager:
) -> Manager2D:

data: Manager = self._get_data_to_aggregate()
data: Manager2D = self._get_data_to_aggregate()

if numeric_only:
data = data.get_numeric_data(copy=False)
Expand Down Expand Up @@ -1691,7 +1697,7 @@ def _wrap_frame_output(self, result, obj: DataFrame) -> DataFrame:
else:
return self.obj._constructor(result, index=obj.index, columns=result_index)

def _get_data_to_aggregate(self) -> Manager:
def _get_data_to_aggregate(self) -> Manager2D:
obj = self._obj_with_exclusions
if self.axis == 1:
return obj.T._mgr
Expand Down Expand Up @@ -1776,7 +1782,7 @@ def _wrap_transformed_output(

return result

def _wrap_agged_manager(self, mgr: Manager) -> DataFrame:
def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
if not self.as_index:
index = np.arange(mgr.shape[1])
mgr.set_axis(1, ibase.Index(index), verify_integrity=False)
Expand Down