Skip to content

BUG: Ensure same index is returned for slow and fast path in groupby.apply #31613

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 5 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`)
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
- Bug in :meth:`DataFrameGroupby.std` and :meth:`DataFrameGroupby.sem` would modify grouped-by columns when ``as_index=False`` (:issue:`10355`)
- 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`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this more user informative, e.g. what case does this happen

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also list the other closed issue here #14927


Reshaping
^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def apply_frame_axis0(object frame, object f, object names,
# Need to infer if low level index slider will cause segfaults
require_slow_apply = i == 0 and piece is chunk
try:
if piece.index is not chunk.index:
if not piece.index.equals(chunk.index):
mutated = True
except AttributeError:
# `piece` might not have an index, could be e.g. an int
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,46 @@ def f_constant_df(group):
assert names == group_names


def test_apply_fast_slow_identical():
# GH 31613

df = DataFrame({"A": [0, 0, 1], "b": range(3)})

# For simple index structures we check for fast/slow apply using
# an identity check on in/output
def slow(group):
return group

def fast(group):
return group.copy()

fast_df = df.groupby("A").apply(fast)
slow_df = df.groupby("A").apply(slow)

tm.assert_frame_equal(fast_df, slow_df)


@pytest.mark.parametrize(
"func",
[
lambda x: x,
lambda x: x[:],
lambda x: x.copy(deep=False),
lambda x: x.copy(deep=True),
],
)
def test_groupby_apply_identity_maybecopy_index_identical(func):
# GH 14927
# Whether the function returns a copy of the input data or not should not
# have an impact on the index structure of the result since this is not
# transparent to the user

df = pd.DataFrame({"g": [1, 2, 2, 2], "a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})

result = df.groupby("g").apply(func)
tm.assert_frame_equal(result, df)


def test_apply_with_mixed_dtype():
# GH3480, apply with mixed dtype on axis=1 breaks in 0.11
df = DataFrame(
Expand Down