|
26 | 26 |
|
27 | 27 | import numpy as np
|
28 | 28 |
|
29 |
| -from pandas._libs import ( |
30 |
| - lib, |
31 |
| - reduction as libreduction, |
32 |
| -) |
| 29 | +from pandas._libs import reduction as libreduction |
33 | 30 | from pandas._typing import (
|
34 | 31 | ArrayLike,
|
35 | 32 | FrameOrSeries,
|
@@ -161,6 +158,19 @@ def pinner(cls):
|
161 | 158 | class SeriesGroupBy(GroupBy[Series]):
|
162 | 159 | _apply_allowlist = base.series_apply_allowlist
|
163 | 160 |
|
| 161 | + def _wrap_agged_manager(self, mgr: Manager2D) -> Series: |
| 162 | + single = mgr.iget(0) |
| 163 | + ser = self.obj._constructor(single, name=self.obj.name) |
| 164 | + ser.index = self.grouper.result_index |
| 165 | + return ser |
| 166 | + |
| 167 | + def _get_data_to_aggregate(self) -> Manager2D: |
| 168 | + obj = self._obj_with_exclusions |
| 169 | + df = obj.to_frame() |
| 170 | + df.columns = [obj.name] # in case name is None, we need to overwrite [0] |
| 171 | + |
| 172 | + return df._mgr |
| 173 | + |
164 | 174 | def _iterate_slices(self) -> Iterable[Series]:
|
165 | 175 | yield self._selected_obj
|
166 | 176 |
|
@@ -768,30 +778,6 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:
|
768 | 778 | out = ensure_int64(out)
|
769 | 779 | return self.obj._constructor(out, index=mi, name=self.obj.name)
|
770 | 780 |
|
771 |
| - def count(self) -> Series: |
772 |
| - """ |
773 |
| - Compute count of group, excluding missing values. |
774 |
| -
|
775 |
| - Returns |
776 |
| - ------- |
777 |
| - Series |
778 |
| - Count of values within each group. |
779 |
| - """ |
780 |
| - ids, _, ngroups = self.grouper.group_info |
781 |
| - val = self.obj._values |
782 |
| - |
783 |
| - mask = (ids != -1) & ~isna(val) |
784 |
| - minlength = ngroups or 0 |
785 |
| - out = np.bincount(ids[mask], minlength=minlength) |
786 |
| - |
787 |
| - result = self.obj._constructor( |
788 |
| - out, |
789 |
| - index=self.grouper.result_index, |
790 |
| - name=self.obj.name, |
791 |
| - dtype="int64", |
792 |
| - ) |
793 |
| - return self._reindex_output(result, fill_value=0) |
794 |
| - |
795 | 781 | @doc(Series.nlargest)
|
796 | 782 | def nlargest(self, n: int = 5, keep: str = "first"):
|
797 | 783 | f = partial(Series.nlargest, n=n, keep=keep)
|
@@ -1583,40 +1569,6 @@ def _apply_to_column_groupbys(self, func, obj: FrameOrSeries) -> DataFrame:
|
1583 | 1569 | else:
|
1584 | 1570 | return concat(results, keys=columns, axis=1)
|
1585 | 1571 |
|
1586 |
| - def count(self) -> DataFrame: |
1587 |
| - """ |
1588 |
| - Compute count of group, excluding missing values. |
1589 |
| -
|
1590 |
| - Returns |
1591 |
| - ------- |
1592 |
| - DataFrame |
1593 |
| - Count of values within each group. |
1594 |
| - """ |
1595 |
| - data = self._get_data_to_aggregate() |
1596 |
| - ids, _, ngroups = self.grouper.group_info |
1597 |
| - mask = ids != -1 |
1598 |
| - |
1599 |
| - def hfunc(bvalues: ArrayLike) -> ArrayLike: |
1600 |
| - # TODO(2DEA): reshape would not be necessary with 2D EAs |
1601 |
| - if bvalues.ndim == 1: |
1602 |
| - # EA |
1603 |
| - masked = mask & ~isna(bvalues).reshape(1, -1) |
1604 |
| - else: |
1605 |
| - masked = mask & ~isna(bvalues) |
1606 |
| - |
1607 |
| - counted = lib.count_level_2d(masked, labels=ids, max_bin=ngroups, axis=1) |
1608 |
| - return counted |
1609 |
| - |
1610 |
| - new_mgr = data.grouped_reduce(hfunc) |
1611 |
| - |
1612 |
| - # If we are grouping on categoricals we want unobserved categories to |
1613 |
| - # return zero, rather than the default of NaN which the reindexing in |
1614 |
| - # _wrap_agged_manager() returns. GH 35028 |
1615 |
| - with com.temp_setattr(self, "observed", True): |
1616 |
| - result = self._wrap_agged_manager(new_mgr) |
1617 |
| - |
1618 |
| - return self._reindex_output(result, fill_value=0) |
1619 |
| - |
1620 | 1572 | def nunique(self, dropna: bool = True) -> DataFrame:
|
1621 | 1573 | """
|
1622 | 1574 | Return DataFrame with counts of unique elements in each position.
|
|
0 commit comments