Skip to content

Commit 7c9042a

Browse files
dsaxtonjreback
authored andcommitted
BUG: Fix groupby.apply (#28662)
1 parent 07ee00e commit 7c9042a

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ Plotting
915915
Groupby/resample/rolling
916916
^^^^^^^^^^^^^^^^^^^^^^^^
917917

918-
-
918+
- Bug in :meth:`DataFrame.groupby.apply` only showing output from a single group when function returns an :class:`Index` (:issue:`28652`)
919919
- Bug in :meth:`DataFrame.groupby` with multiple groups where an ``IndexError`` would be raised if any group contained all NA values (:issue:`20519`)
920920
- Bug in :meth:`pandas.core.resample.Resampler.size` and :meth:`pandas.core.resample.Resampler.count` returning wrong dtype when used with an empty series or dataframe (:issue:`28427`)
921921
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue:`28192`)

pandas/_libs/reduction.pyx

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from copy import copy
12
from distutils.version import LooseVersion
23

34
from cython import Py_ssize_t
@@ -15,7 +16,7 @@ from numpy cimport (ndarray,
1516
cnp.import_array()
1617

1718
cimport pandas._libs.util as util
18-
from pandas._libs.lib import maybe_convert_objects
19+
from pandas._libs.lib import maybe_convert_objects, is_scalar
1920

2021

2122
cdef _check_result_array(object obj, Py_ssize_t cnt):
@@ -492,14 +493,19 @@ def apply_frame_axis0(object frame, object f, object names,
492493
# Need to infer if low level index slider will cause segfaults
493494
require_slow_apply = i == 0 and piece is chunk
494495
try:
495-
if piece.index is chunk.index:
496-
piece = piece.copy(deep='all')
497-
else:
496+
if piece.index is not chunk.index:
498497
mutated = True
499498
except AttributeError:
500499
# `piece` might not have an index, could be e.g. an int
501500
pass
502501

502+
if not is_scalar(piece):
503+
# Need to copy data to avoid appending references
504+
if hasattr(piece, "copy"):
505+
piece = piece.copy(deep="all")
506+
else:
507+
piece = copy(piece)
508+
503509
results.append(piece)
504510

505511
# If the data was modified inplace we need to

pandas/tests/groupby/test_apply.py

+11
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,17 @@ def test_apply_with_mixed_types():
686686
tm.assert_frame_equal(result, expected)
687687

688688

689+
def test_func_returns_object():
690+
# GH 28652
691+
df = DataFrame({"a": [1, 2]}, index=pd.Int64Index([1, 2]))
692+
result = df.groupby("a").apply(lambda g: g.index)
693+
expected = Series(
694+
[pd.Int64Index([1]), pd.Int64Index([2])], index=pd.Int64Index([1, 2], name="a")
695+
)
696+
697+
tm.assert_series_equal(result, expected)
698+
699+
689700
@pytest.mark.parametrize(
690701
"group_column_dtlike",
691702
[datetime.today(), datetime.today().date(), datetime.today().time()],

0 commit comments

Comments
 (0)