Skip to content

Commit 9e4ac71

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 292fcdc + 42a6d44 commit 9e4ac71

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
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

scripts/tests/test_validate_unwanted_patterns.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io
22

33
import pytest
4-
54
import validate_unwanted_patterns
65

76

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ known_dtypes = pandas.core.dtypes
105105
known_post_core = pandas.tseries,pandas.io,pandas.plotting
106106
sections = FUTURE,STDLIB,THIRDPARTY,PRE_LIBS,PRE_CORE,DTYPES,FIRSTPARTY,POST_CORE,LOCALFOLDER
107107
known_first_party = pandas
108-
known_third_party = _pytest,announce,dateutil,docutils,flake8,git,hypothesis,jinja2,lxml,matplotlib,numpy,numpydoc,pkg_resources,pyarrow,pytest,pytz,requests,scipy,setuptools,sphinx,sqlalchemy,validate_docstrings,yaml,odf
108+
known_third_party = _pytest,announce,dateutil,docutils,flake8,git,hypothesis,jinja2,lxml,matplotlib,numpy,numpydoc,pkg_resources,pyarrow,pytest,pytz,requests,scipy,setuptools,sphinx,sqlalchemy,validate_docstrings,validate_unwanted_patterns,yaml,odf
109109
multi_line_output = 3
110110
include_trailing_comma = True
111111
force_grid_wrap = 0

0 commit comments

Comments
 (0)