Skip to content

Commit 2f15d1c

Browse files
authored
REF: reuse _combine instead of reset_dropped_locs (pandas-dev#35884)
1 parent e87b8bc commit 2f15d1c

File tree

3 files changed

+7
-45
lines changed

3 files changed

+7
-45
lines changed

pandas/core/groupby/generic.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
Mapping,
2222
Optional,
2323
Sequence,
24-
Tuple,
2524
Type,
2625
Union,
2726
)
@@ -1025,16 +1024,14 @@ def _iterate_slices(self) -> Iterable[Series]:
10251024
def _cython_agg_general(
10261025
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
10271026
) -> DataFrame:
1028-
agg_blocks, agg_items = self._cython_agg_blocks(
1027+
agg_mgr = self._cython_agg_blocks(
10291028
how, alt=alt, numeric_only=numeric_only, min_count=min_count
10301029
)
1031-
return self._wrap_agged_blocks(agg_blocks, items=agg_items)
1030+
return self._wrap_agged_blocks(agg_mgr.blocks, items=agg_mgr.items)
10321031

10331032
def _cython_agg_blocks(
10341033
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
1035-
) -> "Tuple[List[Block], Index]":
1036-
# TODO: the actual managing of mgr_locs is a PITA
1037-
# here, it should happen via BlockManager.combine
1034+
) -> BlockManager:
10381035

10391036
data: BlockManager = self._get_data_to_aggregate()
10401037

@@ -1124,15 +1121,14 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike:
11241121
res_values = cast_agg_result(result, bvalues, how)
11251122
return res_values
11261123

1127-
skipped: List[int] = []
11281124
for i, block in enumerate(data.blocks):
11291125
try:
11301126
nbs = block.apply(blk_func)
11311127
except (NotImplementedError, TypeError):
11321128
# TypeError -> we may have an exception in trying to aggregate
11331129
# continue and exclude the block
11341130
# NotImplementedError -> "ohlc" with wrong dtype
1135-
skipped.append(i)
1131+
pass
11361132
else:
11371133
agg_blocks.extend(nbs)
11381134

@@ -1141,9 +1137,8 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike:
11411137

11421138
# reset the locs in the blocks to correspond to our
11431139
# current ordering
1144-
agg_items = data.reset_dropped_locs(agg_blocks, skipped)
1145-
1146-
return agg_blocks, agg_items
1140+
new_mgr = data._combine(agg_blocks)
1141+
return new_mgr
11471142

11481143
def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
11491144
if self.grouper.nkeys != 1:

pandas/core/internals/managers.py

-32
Original file line numberDiff line numberDiff line change
@@ -1486,38 +1486,6 @@ def unstack(self, unstacker, fill_value) -> "BlockManager":
14861486
bm = BlockManager(new_blocks, [new_columns, new_index])
14871487
return bm
14881488

1489-
def reset_dropped_locs(self, blocks: List[Block], skipped: List[int]) -> Index:
1490-
"""
1491-
Decrement the mgr_locs of the given blocks with `skipped` removed.
1492-
1493-
Notes
1494-
-----
1495-
Alters each block's mgr_locs inplace.
1496-
"""
1497-
ncols = len(self)
1498-
1499-
new_locs = [blk.mgr_locs.as_array for blk in blocks]
1500-
indexer = np.concatenate(new_locs)
1501-
1502-
new_items = self.items.take(np.sort(indexer))
1503-
1504-
if skipped:
1505-
# we need to adjust the indexer to account for the
1506-
# items we have removed
1507-
deleted_items = [self.blocks[i].mgr_locs.as_array for i in skipped]
1508-
deleted = np.concatenate(deleted_items)
1509-
ai = np.arange(ncols)
1510-
mask = np.zeros(ncols)
1511-
mask[deleted] = 1
1512-
indexer = (ai - mask.cumsum())[indexer]
1513-
1514-
offset = 0
1515-
for blk in blocks:
1516-
loc = len(blk.mgr_locs)
1517-
blk.mgr_locs = indexer[offset : (offset + loc)]
1518-
offset += loc
1519-
return new_items
1520-
15211489

15221490
class SingleBlockManager(BlockManager):
15231491
""" manage a single block with """

pandas/core/window/rolling.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike:
561561
elif not len(res_blocks):
562562
return obj.astype("float64")
563563

564-
new_cols = mgr.reset_dropped_locs(res_blocks, skipped)
565-
new_mgr = type(mgr).from_blocks(res_blocks, [new_cols, obj.index])
564+
new_mgr = mgr._combine(res_blocks)
566565
out = obj._constructor(new_mgr)
567566
self._insert_on_column(out, obj)
568567
return out

0 commit comments

Comments
 (0)