Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ def _wrap_applied_output(
if is_transform:
# GH#47787 see test_group_on_empty_multiindex
res_index = data.index
elif not self.group_keys:
elif not self.group_keys or not self.as_index:
res_index = None
else:
res_index = self._grouper.result_index
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,18 @@ def addstore(x):

tm.assert_frame_equal(store[0], expected_out_0)
tm.assert_frame_equal(store[1], expected_out_1)


def test_apply_as_index_false_empty_index_name():
Comment thread
Ashish-ml-eng marked this conversation as resolved.
df = DataFrame({"A": [4, 4, 4], "B": [9, 9, 9]})
result = df.groupby("A", as_index=False).apply(lambda x: x)

empty_df = df.iloc[:0]
result_empty = empty_df.groupby("A", as_index=False).apply(lambda x: x)

assert result.index.names == [None]
assert result_empty.index.names == result.index.names, (
f"empty_names={result_empty.index.names}, "
f"nonempty_names={result.index.names}, "
f"empty_index_name={result_empty.index.name}"
)
Comment on lines +1551 to +1558
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you assert the full result.

expected = DataFrame(...)
tm.assert_frame_equal(result, expected)

Loading