Skip to content

Commit da5693b

Browse files
committed
groupy apply: Ensure same index is returned for slow and fast path
1 parent cb35d8a commit da5693b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ Groupby/resample/rolling
827827
- Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`)
828828
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
829829
- Bug in :meth:`DataFrameGroupby.std` and :meth:`DataFrameGroupby.sem` would modify grouped-by columns when ``as_index=False`` (:issue:`10355`)
830+
- Bug in :meth:`core.groupby.DataFrameGroupBy.apply` where the result shape was incorrect when the output index was not identical to the input index (:issue:`31612`)
830831

831832
Reshaping
832833
^^^^^^^^^

pandas/_libs/reduction.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def apply_frame_axis0(object frame, object f, object names,
502502
# Need to infer if low level index slider will cause segfaults
503503
require_slow_apply = i == 0 and piece is chunk
504504
try:
505-
if piece.index is not chunk.index:
505+
if not piece.index.equals(chunk.index):
506506
mutated = True
507507
except AttributeError:
508508
# `piece` might not have an index, could be e.g. an int

pandas/tests/groupby/test_apply.py

+19
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,22 @@ def fn(x):
921921
name="col2",
922922
)
923923
tm.assert_series_equal(result, expected)
924+
925+
926+
def test_apply_fast_slow_identical():
927+
# GH 31613
928+
929+
df = DataFrame({"A": [0, 0, 1], "b": range(3)})
930+
931+
# For simple index structures we check for fast/slow apply using
932+
# an identity check on in/output
933+
def slow(group):
934+
return group
935+
936+
def fast(group):
937+
return group.copy()
938+
939+
fast_df = df.groupby("A").apply(fast)
940+
slow_df = df.groupby("A").apply(slow)
941+
942+
tm.assert_frame_equal(fast_df, slow_df)

0 commit comments

Comments
 (0)