Skip to content

Commit 7a36c79

Browse files
topper-123jreback
authored andcommitted
CLN: DataFrameGroupBy._cython_agg_general (#30384)
1 parent e745be0 commit 7a36c79

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

pandas/core/groupby/generic.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from textwrap import dedent
1212
import typing
1313
from typing import (
14+
TYPE_CHECKING,
1415
Any,
1516
Callable,
1617
FrozenSet,
@@ -68,6 +69,10 @@
6869

6970
from pandas.plotting import boxplot_frame_groupby
7071

72+
if TYPE_CHECKING:
73+
from pandas.core.internals import Block
74+
75+
7176
NamedAgg = namedtuple("NamedAgg", ["column", "aggfunc"])
7277
# TODO(typing) the return value on this callable should be any *scalar*.
7378
AggScalar = Union[str, Callable[..., Any]]
@@ -987,11 +992,11 @@ def _iterate_slices(self) -> Iterable[Series]:
987992

988993
def _cython_agg_general(
989994
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
990-
):
991-
new_items, new_blocks = self._cython_agg_blocks(
995+
) -> DataFrame:
996+
agg_items, agg_blocks = self._cython_agg_blocks(
992997
how, alt=alt, numeric_only=numeric_only, min_count=min_count
993998
)
994-
return self._wrap_agged_blocks(new_items, new_blocks)
999+
return self._wrap_agged_blocks(agg_blocks, items=agg_items)
9951000

9961001
def _cython_agg_blocks(
9971002
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
@@ -1691,17 +1696,17 @@ def _wrap_transformed_output(
16911696

16921697
return result
16931698

1694-
def _wrap_agged_blocks(self, items, blocks):
1699+
def _wrap_agged_blocks(self, blocks: "Sequence[Block]", items: Index) -> DataFrame:
16951700
if not self.as_index:
16961701
index = np.arange(blocks[0].values.shape[-1])
1697-
mgr = BlockManager(blocks, [items, index])
1702+
mgr = BlockManager(blocks, axes=[items, index])
16981703
result = DataFrame(mgr)
16991704

17001705
self._insert_inaxis_grouper_inplace(result)
17011706
result = result._consolidate()
17021707
else:
17031708
index = self.grouper.result_index
1704-
mgr = BlockManager(blocks, [items, index])
1709+
mgr = BlockManager(blocks, axes=[items, index])
17051710
result = DataFrame(mgr)
17061711

17071712
if self.axis == 1:
@@ -1740,18 +1745,18 @@ def count(self):
17401745
ids, _, ngroups = self.grouper.group_info
17411746
mask = ids != -1
17421747

1743-
val = (
1748+
vals = (
17441749
(mask & ~_isna_ndarraylike(np.atleast_2d(blk.get_values())))
17451750
for blk in data.blocks
17461751
)
1747-
loc = (blk.mgr_locs for blk in data.blocks)
1752+
locs = (blk.mgr_locs for blk in data.blocks)
17481753

1749-
counted = [
1750-
lib.count_level_2d(x, labels=ids, max_bin=ngroups, axis=1) for x in val
1751-
]
1752-
blk = map(make_block, counted, loc)
1754+
counted = (
1755+
lib.count_level_2d(x, labels=ids, max_bin=ngroups, axis=1) for x in vals
1756+
)
1757+
blocks = [make_block(val, placement=loc) for val, loc in zip(counted, locs)]
17531758

1754-
return self._wrap_agged_blocks(data.items, list(blk))
1759+
return self._wrap_agged_blocks(blocks, items=data.items)
17551760

17561761
def nunique(self, dropna: bool = True):
17571762
"""

0 commit comments

Comments
 (0)