Skip to content

Commit ee85e5a

Browse files
committed
Changed impl; fixed test
1 parent b694b09 commit ee85e5a

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

pandas/core/groupby/generic.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -1509,23 +1509,19 @@ def count(self):
15091509
DataFrame
15101510
Count of values within each group.
15111511
"""
1512-
obj = self._selected_obj
1513-
1514-
def groupby_series(obj, col=None):
1515-
return SeriesGroupBy(obj, selection=col, grouper=self.grouper).count()
1516-
1517-
if isinstance(obj, Series):
1518-
results = groupby_series(obj)
1519-
else:
1520-
from pandas.core.reshape.concat import concat
1512+
output = OrderedDict()
15211513

1522-
results = [groupby_series(obj[col], col) for col in obj.columns]
1523-
results = concat(results, axis=1)
1524-
results.columns.names = obj.columns.names
1525-
1526-
if not self.as_index:
1527-
results.index = ibase.default_index(len(results))
1528-
return results
1514+
# TODO: dispatch to _cython_agg_general instead of custom looping
1515+
# TODO: refactor with series logic
1516+
ids, _, ngroups = self.grouper.group_info
1517+
for name, obj in self._iterate_slices():
1518+
mask = (ids != -1) & ~isna(obj)
1519+
ids = ensure_platform_int(ids)
1520+
minlength = ngroups or 0
1521+
out = np.bincount(ids[mask], minlength=minlength)
1522+
output[name] = out
1523+
1524+
return self._wrap_aggregated_output(output)
15291525

15301526
def nunique(self, dropna=True):
15311527
"""

pandas/tests/groupby/test_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_observed(observed):
300300
exp_index = CategoricalIndex(
301301
list("ab"), name="cat", categories=list("abc"), ordered=True
302302
)
303-
expected = DataFrame({"ints": [1.5, 1.5], "val": [20.0, 30]}, index=exp_index)
303+
expected = DataFrame({"ints": [1.5, 1.5], "val": [20, 30]}, index=exp_index)
304304
if not observed:
305305
index = CategoricalIndex(
306306
list("abc"), name="cat", categories=list("abc"), ordered=True

0 commit comments

Comments
 (0)