|
77 | 77 | from pandas.core.groupby.numba_ import generate_numba_func, split_for_numba
|
78 | 78 | from pandas.core.indexes.api import Index, MultiIndex, all_indexes_same
|
79 | 79 | import pandas.core.indexes.base as ibase
|
80 |
| -from pandas.core.internals import BlockManager, make_block |
| 80 | +from pandas.core.internals import BlockManager |
81 | 81 | from pandas.core.series import Series
|
82 | 82 | from pandas.core.util.numba_ import NUMBA_FUNC_CACHE, maybe_use_numba
|
83 | 83 |
|
@@ -1750,20 +1750,24 @@ def count(self) -> DataFrame:
|
1750 | 1750 | ids, _, ngroups = self.grouper.group_info
|
1751 | 1751 | mask = ids != -1
|
1752 | 1752 |
|
1753 |
| - # TODO(2DEA): reshape would not be necessary with 2D EAs |
1754 |
| - vals = ((mask & ~isna(blk.values).reshape(blk.shape)) for blk in data.blocks) |
1755 |
| - locs = (blk.mgr_locs for blk in data.blocks) |
| 1753 | + def hfunc(bvalues: ArrayLike) -> ArrayLike: |
| 1754 | + # TODO(2DEA): reshape would not be necessary with 2D EAs |
| 1755 | + if bvalues.ndim == 1: |
| 1756 | + # EA |
| 1757 | + masked = mask & ~isna(bvalues).reshape(1, -1) |
| 1758 | + else: |
| 1759 | + masked = mask & ~isna(bvalues) |
1756 | 1760 |
|
1757 |
| - counted = ( |
1758 |
| - lib.count_level_2d(x, labels=ids, max_bin=ngroups, axis=1) for x in vals |
1759 |
| - ) |
1760 |
| - blocks = [make_block(val, placement=loc) for val, loc in zip(counted, locs)] |
| 1761 | + counted = lib.count_level_2d(masked, labels=ids, max_bin=ngroups, axis=1) |
| 1762 | + return counted |
| 1763 | + |
| 1764 | + new_mgr = data.apply(hfunc) |
1761 | 1765 |
|
1762 | 1766 | # If we are grouping on categoricals we want unobserved categories to
|
1763 | 1767 | # return zero, rather than the default of NaN which the reindexing in
|
1764 | 1768 | # _wrap_agged_blocks() returns. GH 35028
|
1765 | 1769 | with com.temp_setattr(self, "observed", True):
|
1766 |
| - result = self._wrap_agged_blocks(blocks, items=data.items) |
| 1770 | + result = self._wrap_agged_blocks(new_mgr.blocks, items=data.items) |
1767 | 1771 |
|
1768 | 1772 | return self._reindex_output(result, fill_value=0)
|
1769 | 1773 |
|
|
0 commit comments