|
15 | 15 | Any,
|
16 | 16 | Callable,
|
17 | 17 | Hashable,
|
18 |
| - Iterable, |
19 | 18 | Literal,
|
20 | 19 | Mapping,
|
21 | 20 | NamedTuple,
|
@@ -1337,38 +1336,27 @@ def _python_agg_general(self, func, *args, **kwargs):
|
1337 | 1336 | func = com.is_builtin_func(func)
|
1338 | 1337 | f = lambda x: func(x, *args, **kwargs)
|
1339 | 1338 |
|
1340 |
| - # iterate through "columns" ex exclusions to populate output dict |
1341 |
| - output: dict[base.OutputKey, ArrayLike] = {} |
1342 |
| - |
1343 | 1339 | if self.ngroups == 0:
|
1344 | 1340 | # e.g. test_evaluate_with_empty_groups different path gets different
|
1345 | 1341 | # result dtype in empty case.
|
1346 | 1342 | return self._python_apply_general(f, self._selected_obj, is_agg=True)
|
1347 | 1343 |
|
1348 |
| - for idx, obj in enumerate(self._iterate_slices()): |
1349 |
| - name = obj.name |
1350 |
| - result = self.grouper.agg_series(obj, f) |
1351 |
| - key = base.OutputKey(label=name, position=idx) |
1352 |
| - output[key] = result |
| 1344 | + obj = self._obj_with_exclusions |
| 1345 | + if self.axis == 1: |
| 1346 | + obj = obj.T |
1353 | 1347 |
|
1354 |
| - if not output: |
| 1348 | + if not len(obj.columns): |
1355 | 1349 | # e.g. test_margins_no_values_no_cols
|
1356 | 1350 | return self._python_apply_general(f, self._selected_obj)
|
1357 | 1351 |
|
1358 |
| - res = self._indexed_output_to_ndframe(output) |
1359 |
| - return self._wrap_aggregated_output(res) |
1360 |
| - |
1361 |
| - def _iterate_slices(self) -> Iterable[Series]: |
1362 |
| - obj = self._obj_with_exclusions |
1363 |
| - if self.axis == 1: |
1364 |
| - obj = obj.T |
| 1352 | + output: dict[int, ArrayLike] = {} |
| 1353 | + for idx, (name, ser) in enumerate(obj.items()): |
| 1354 | + result = self.grouper.agg_series(ser, f) |
| 1355 | + output[idx] = result |
1365 | 1356 |
|
1366 |
| - if isinstance(obj, Series): |
1367 |
| - # Occurs when doing DataFrameGroupBy(...)["X"] |
1368 |
| - yield obj |
1369 |
| - else: |
1370 |
| - for label, values in obj.items(): |
1371 |
| - yield values |
| 1357 | + res = self.obj._constructor(output) |
| 1358 | + res.columns = obj.columns.copy(deep=False) |
| 1359 | + return self._wrap_aggregated_output(res) |
1372 | 1360 |
|
1373 | 1361 | def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
|
1374 | 1362 | if self.grouper.nkeys != 1:
|
@@ -1830,20 +1818,6 @@ def _get_data_to_aggregate(
|
1830 | 1818 | mgr = mgr.get_numeric_data(copy=False)
|
1831 | 1819 | return mgr
|
1832 | 1820 |
|
1833 |
| - def _indexed_output_to_ndframe( |
1834 |
| - self, output: Mapping[base.OutputKey, ArrayLike] |
1835 |
| - ) -> DataFrame: |
1836 |
| - """ |
1837 |
| - Wrap the dict result of a GroupBy aggregation into a DataFrame. |
1838 |
| - """ |
1839 |
| - indexed_output = {key.position: val for key, val in output.items()} |
1840 |
| - columns = Index([key.label for key in output]) |
1841 |
| - columns._set_names(self._obj_with_exclusions._get_axis(1 - self.axis).names) |
1842 |
| - |
1843 |
| - result = self.obj._constructor(indexed_output) |
1844 |
| - result.columns = columns |
1845 |
| - return result |
1846 |
| - |
1847 | 1821 | def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
|
1848 | 1822 | return self.obj._constructor(mgr)
|
1849 | 1823 |
|
|
0 commit comments