Skip to content

REF: use BlockManager.apply for DataFrameGroupBy.count #35924

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 17 commits into from
Sep 4, 2020
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
4c5eddd
REF: remove unnecesary try/except
jbrockmendel Aug 21, 2020
c632c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
9e64be3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Aug 21, 2020
42649fb
TST: add test for agg on ordered categorical cols (#35630)
mathurk1 Aug 21, 2020
47121dd
TST: resample does not yield empty groups (#10603) (#35799)
tkmz-n Aug 21, 2020
1decb3e
revert accidental rebase
jbrockmendel Aug 22, 2020
57c5dd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 22, 2020
a358463
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
ffa7ad7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 23, 2020
e5e98d4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
408db5a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 24, 2020
d3493cf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
75a805a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
9f61070
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 25, 2020
2d10f6e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
3e20187
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
jbrockmendel Aug 26, 2020
997393a
REF: use BlockManager.apply for DataFrameGroupBy.count
jbrockmendel Aug 27, 2020
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
22 changes: 13 additions & 9 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
from pandas.core.groupby.numba_ import generate_numba_func, split_for_numba
from pandas.core.indexes.api import Index, MultiIndex, all_indexes_same
import pandas.core.indexes.base as ibase
from pandas.core.internals import BlockManager, make_block
from pandas.core.internals import BlockManager
from pandas.core.series import Series
from pandas.core.util.numba_ import NUMBA_FUNC_CACHE, maybe_use_numba

Expand Down Expand Up @@ -1765,20 +1765,24 @@ def count(self):
ids, _, ngroups = self.grouper.group_info
mask = ids != -1

# TODO(2DEA): reshape would not be necessary with 2D EAs
vals = ((mask & ~isna(blk.values).reshape(blk.shape)) for blk in data.blocks)
locs = (blk.mgr_locs for blk in data.blocks)
def hfunc(bvalues: ArrayLike) -> ArrayLike:
# TODO(2DEA): reshape would not be necessary with 2D EAs
if bvalues.ndim == 1:
# EA
masked = mask & ~isna(bvalues).reshape(1, -1)
else:
masked = mask & ~isna(bvalues)

counted = (
lib.count_level_2d(x, labels=ids, max_bin=ngroups, axis=1) for x in vals
)
blocks = [make_block(val, placement=loc) for val, loc in zip(counted, locs)]
counted = lib.count_level_2d(masked, labels=ids, max_bin=ngroups, axis=1)
return counted

new_mgr = data.apply(hfunc)

# If we are grouping on categoricals we want unobserved categories to
# return zero, rather than the default of NaN which the reindexing in
# _wrap_agged_blocks() returns. GH 35028
with com.temp_setattr(self, "observed", True):
result = self._wrap_agged_blocks(blocks, items=data.items)
result = self._wrap_agged_blocks(new_mgr.blocks, items=data.items)

return self._reindex_output(result, fill_value=0)

Expand Down