Skip to content

Commit 08eca21

Browse files
authored
TYP, DOC, CLN:SeriesGroupBy._wrap_applied_output (#35120)
1 parent 031fb16 commit 08eca21

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

pandas/core/groupby/generic.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Iterable,
2020
List,
2121
Mapping,
22+
Optional,
2223
Sequence,
2324
Tuple,
2425
Type,
@@ -30,7 +31,7 @@
3031
import numpy as np
3132

3233
from pandas._libs import lib
33-
from pandas._typing import FrameOrSeries
34+
from pandas._typing import FrameOrSeries, FrameOrSeriesUnion
3435
from pandas.util._decorators import Appender, Substitution, doc
3536

3637
from pandas.core.dtypes.cast import (
@@ -413,12 +414,31 @@ def _wrap_transformed_output(
413414
assert isinstance(result, Series)
414415
return result
415416

416-
def _wrap_applied_output(self, keys, values, not_indexed_same=False):
417+
def _wrap_applied_output(
418+
self, keys: Index, values: Optional[List[Any]], not_indexed_same: bool = False
419+
) -> FrameOrSeriesUnion:
420+
"""
421+
Wrap the output of SeriesGroupBy.apply into the expected result.
422+
423+
Parameters
424+
----------
425+
keys : Index
426+
Keys of groups that Series was grouped by.
427+
values : Optional[List[Any]]
428+
Applied output for each group.
429+
not_indexed_same : bool, default False
430+
Whether the applied outputs are not indexed the same as the group axes.
431+
432+
Returns
433+
-------
434+
DataFrame or Series
435+
"""
417436
if len(keys) == 0:
418437
# GH #6265
419438
return self.obj._constructor(
420439
[], name=self._selection_name, index=keys, dtype=np.float64
421440
)
441+
assert values is not None
422442

423443
def _get_index() -> Index:
424444
if self.grouper.nkeys > 1:
@@ -430,19 +450,15 @@ def _get_index() -> Index:
430450
if isinstance(values[0], dict):
431451
# GH #823 #24880
432452
index = _get_index()
433-
result = self._reindex_output(
453+
result: FrameOrSeriesUnion = self._reindex_output(
434454
self.obj._constructor_expanddim(values, index=index)
435455
)
436456
# if self.observed is False,
437457
# keep all-NaN rows created while re-indexing
438458
result = result.stack(dropna=self.observed)
439459
result.name = self._selection_name
440460
return result
441-
442-
if isinstance(values[0], Series):
443-
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
444-
elif isinstance(values[0], DataFrame):
445-
# possible that Series -> DataFrame by applied function
461+
elif isinstance(values[0], (Series, DataFrame)):
446462
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
447463
else:
448464
# GH #6265 #24880

0 commit comments

Comments
 (0)