Skip to content

Commit edefc27

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

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v1.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Deprecations
4141

4242
Bug fixes
4343
~~~~~~~~~
44+
- 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`)
4445

4546
**Datetimelike**
4647

pandas/_libs/reduction.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def apply_frame_axis0(object frame, object f, object names,
493493
# Need to infer if low level index slider will cause segfaults
494494
require_slow_apply = i == 0 and piece is chunk
495495
try:
496-
if piece.index is not chunk.index:
496+
if not piece.index.equals(chunk.index):
497497
mutated = True
498498
except AttributeError:
499499
# `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
@@ -851,3 +851,22 @@ def test_apply_function_returns_non_pandas_non_scalar(function, expected_values)
851851
result = df.groupby("groups").apply(function)
852852
expected = pd.Series(expected_values, index=pd.Index(["A", "B"], name="groups"))
853853
tm.assert_series_equal(result, expected)
854+
855+
856+
def test_apply_fast_slow_identical():
857+
# GH 31613
858+
859+
df = DataFrame({"A": [0, 0, 1], "b": range(3)})
860+
861+
# For simple index structures we check for fast/slow apply using
862+
# an identity check on in/output
863+
def slow(group):
864+
return group
865+
866+
def fast(group):
867+
return group.copy()
868+
869+
fast_df = df.groupby("A").apply(fast)
870+
slow_df = df.groupby("A").apply(slow)
871+
872+
tm.assert_frame_equal(fast_df, slow_df)

0 commit comments

Comments
 (0)