Skip to content

Commit 805f0d9

Browse files
authored
REF: dont pass keys through wrap_applied_output (#43479)
1 parent 7d790cf commit 805f0d9

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

pandas/core/groupby/generic.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ def _indexed_output_to_ndframe(
369369
def _wrap_applied_output(
370370
self,
371371
data: Series,
372-
keys: Index,
373372
values: list[Any] | None,
374373
not_indexed_same: bool = False,
375374
) -> DataFrame | Series:
@@ -380,8 +379,6 @@ def _wrap_applied_output(
380379
----------
381380
data : Series
382381
Input data for groupby operation.
383-
keys : Index
384-
Keys of groups that Series was grouped by.
385382
values : Optional[List[Any]]
386383
Applied output for each group.
387384
not_indexed_same : bool, default False
@@ -391,6 +388,8 @@ def _wrap_applied_output(
391388
-------
392389
DataFrame or Series
393390
"""
391+
keys = self.grouper.group_keys_seq
392+
394393
if len(keys) == 0:
395394
# GH #6265
396395
return self.obj._constructor(
@@ -412,7 +411,7 @@ def _wrap_applied_output(
412411
res_ser.name = self.obj.name
413412
return res_ser
414413
elif isinstance(values[0], (Series, DataFrame)):
415-
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
414+
return self._concat_objects(values, not_indexed_same=not_indexed_same)
416415
else:
417416
# GH #6265 #24880
418417
result = self.obj._constructor(
@@ -1100,7 +1099,9 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame:
11001099
res_df.columns = obj.columns
11011100
return res_df
11021101

1103-
def _wrap_applied_output(self, data, keys, values, not_indexed_same=False):
1102+
def _wrap_applied_output(self, data, values, not_indexed_same=False):
1103+
keys = self.grouper.group_keys_seq
1104+
11041105
if len(keys) == 0:
11051106
result = self.obj._constructor(
11061107
index=self.grouper.result_index, columns=data.columns
@@ -1115,7 +1116,7 @@ def _wrap_applied_output(self, data, keys, values, not_indexed_same=False):
11151116
# GH9684 - All values are None, return an empty frame.
11161117
return self.obj._constructor()
11171118
elif isinstance(first_not_none, DataFrame):
1118-
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
1119+
return self._concat_objects(values, not_indexed_same=not_indexed_same)
11191120

11201121
key_index = self.grouper.result_index if self.as_index else None
11211122

@@ -1143,12 +1144,11 @@ def _wrap_applied_output(self, data, keys, values, not_indexed_same=False):
11431144
else:
11441145
# values are Series
11451146
return self._wrap_applied_output_series(
1146-
keys, values, not_indexed_same, first_not_none, key_index
1147+
values, not_indexed_same, first_not_none, key_index
11471148
)
11481149

11491150
def _wrap_applied_output_series(
11501151
self,
1151-
keys,
11521152
values: list[Series],
11531153
not_indexed_same: bool,
11541154
first_not_none,
@@ -1171,6 +1171,7 @@ def _wrap_applied_output_series(
11711171

11721172
# assign the name to this series
11731173
if singular_series:
1174+
keys = self.grouper.group_keys_seq
11741175
values[0].name = keys[0]
11751176

11761177
# GH2893
@@ -1179,9 +1180,7 @@ def _wrap_applied_output_series(
11791180
# if any of the sub-series are not indexed the same
11801181
# OR we don't have a multi-index and we have only a
11811182
# single values
1182-
return self._concat_objects(
1183-
keys, values, not_indexed_same=not_indexed_same
1184-
)
1183+
return self._concat_objects(values, not_indexed_same=not_indexed_same)
11851184

11861185
# still a series
11871186
# path added as of GH 5545
@@ -1192,7 +1191,7 @@ def _wrap_applied_output_series(
11921191

11931192
if not all_indexed_same:
11941193
# GH 8467
1195-
return self._concat_objects(keys, values, not_indexed_same=True)
1194+
return self._concat_objects(values, not_indexed_same=True)
11961195

11971196
# Combine values
11981197
# vstack+constructor is faster than concat and handles MI-columns

pandas/core/groupby/groupby.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ def _iterate_slices(self) -> Iterable[Series]:
998998
# Dispatch/Wrapping
999999

10001000
@final
1001-
def _concat_objects(self, keys, values, not_indexed_same: bool = False):
1001+
def _concat_objects(self, values, not_indexed_same: bool = False):
10021002
from pandas.core.reshape.concat import concat
10031003

10041004
def reset_identity(values):
@@ -1035,7 +1035,7 @@ def reset_identity(values):
10351035
if self.as_index:
10361036

10371037
# possible MI return case
1038-
group_keys = keys
1038+
group_keys = self.grouper.group_keys_seq
10391039
group_levels = self.grouper.levels
10401040
group_names = self.grouper.names
10411041

@@ -1171,7 +1171,7 @@ def _wrap_transformed_output(
11711171
result.index = self.obj.index
11721172
return result
11731173

1174-
def _wrap_applied_output(self, data, keys, values, not_indexed_same: bool = False):
1174+
def _wrap_applied_output(self, data, values, not_indexed_same: bool = False):
11751175
raise AbstractMethodError(self)
11761176

11771177
def _resolve_numeric_only(self, numeric_only: bool | lib.NoDefault) -> bool:
@@ -1215,7 +1215,7 @@ def _group_keys_index(self) -> Index:
12151215
# The index to use for the result of Groupby Aggregations.
12161216
# This _may_ be redundant with self.grouper.result_index, but that
12171217
# has not been conclusively proven yet.
1218-
keys = self.grouper._get_group_keys()
1218+
keys = self.grouper.group_keys_seq
12191219
if self.grouper.nkeys > 1:
12201220
index = MultiIndex.from_tuples(keys, names=self.grouper.names)
12211221
else:
@@ -1256,7 +1256,7 @@ def _transform_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs)
12561256
data and indices into a Numba jitted function.
12571257
"""
12581258
starts, ends, sorted_index, sorted_data = self._numba_prep(func, data)
1259-
group_keys = self.grouper._get_group_keys()
1259+
group_keys = self.grouper.group_keys_seq
12601260

12611261
numba_transform_func = numba_.generate_numba_transform_func(
12621262
kwargs, func, engine_kwargs
@@ -1393,13 +1393,13 @@ def _python_apply_general(
13931393
Series or DataFrame
13941394
data after applying f
13951395
"""
1396-
keys, values, mutated = self.grouper.apply(f, data, self.axis)
1396+
values, mutated = self.grouper.apply(f, data, self.axis)
13971397

13981398
if not_indexed_same is None:
13991399
not_indexed_same = mutated or self.mutated
14001400

14011401
return self._wrap_applied_output(
1402-
data, keys, values, not_indexed_same=not_indexed_same
1402+
data, values, not_indexed_same=not_indexed_same
14031403
)
14041404

14051405
@final

pandas/core/groupby/ops.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def get_iterator(
690690
for each group
691691
"""
692692
splitter = self._get_splitter(data, axis=axis)
693-
keys = self._get_group_keys()
693+
keys = self.group_keys_seq
694694
for key, group in zip(keys, splitter):
695695
yield key, group.__finalize__(data, method="groupby")
696696

@@ -716,7 +716,8 @@ def _get_grouper(self):
716716
return self.groupings[0].grouping_vector
717717

718718
@final
719-
def _get_group_keys(self):
719+
@cache_readonly
720+
def group_keys_seq(self):
720721
if len(self.groupings) == 1:
721722
return self.levels[0]
722723
else:
@@ -726,10 +727,10 @@ def _get_group_keys(self):
726727
return get_flattened_list(ids, ngroups, self.levels, self.codes)
727728

728729
@final
729-
def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
730+
def apply(self, f: F, data: FrameOrSeries, axis: int = 0) -> tuple[list, bool]:
730731
mutated = self.mutated
731732
splitter = self._get_splitter(data, axis=axis)
732-
group_keys = self._get_group_keys()
733+
group_keys = self.group_keys_seq
733734
result_values = []
734735

735736
# This calls DataSplitter.__iter__
@@ -745,7 +746,7 @@ def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
745746
mutated = True
746747
result_values.append(res)
747748

748-
return group_keys, result_values, mutated
749+
return result_values, mutated
749750

750751
@cache_readonly
751752
def indices(self):

0 commit comments

Comments
 (0)