Skip to content

BUG: Fix groupby.apply #28662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 1, 2020
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

-
- Bug in :meth:`DataFrame.groupby.apply` only returning output from a single group in some cases (:issue:`28652`)
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
- Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`)
- Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import copy
from distutils.version import LooseVersion

from cython import Py_ssize_t
Expand All @@ -16,6 +17,7 @@ cnp.import_array()

cimport pandas._libs.util as util
from pandas._libs.lib import maybe_convert_objects, values_from_object
from pandas.api.types import is_scalar


cdef _get_result_array(object obj, Py_ssize_t size, Py_ssize_t cnt):
Expand Down Expand Up @@ -543,7 +545,10 @@ def apply_frame_axis0(object frame, object f, object names,
# `piece` might not have an index, could be e.g. an int
pass

results.append(piece)
if is_scalar(piece):
results.append(piece)
else:
results.append(copy(piece))

# If the data was modified inplace we need to
# take the slow path to not risk segfaults
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,3 +1357,14 @@ def test_frequency_is_original(self, num_cols):
df = DataFrame(1, index=index, columns=range(num_cols))
df.apply(lambda x: x)
assert index.freq == original.freq

def test_func_returns_object(self):
# GH 28652
df = DataFrame({"a": [1, 2]}, index=pd.Int64Index([1, 2]))
result = df.groupby("a").apply(lambda g: g.index)
expected = Series(
[pd.Int64Index([1]), pd.Int64Index([2])],
index=pd.Int64Index([1, 2], name="a"),
)

tm.assert_series_equal(result, expected)