Skip to content

REGR: groupby with as_index=False on an empty frame #42254

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 3 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,9 @@ def _wrap_transformed_output(

def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
if not self.as_index:
index = Index(range(mgr.shape[1]))
# GH 41998 - empty mgr always gets index of length 0
rows = mgr.shape[1] if mgr.shape[0] > 0 else 0
Copy link
Member

Choose a reason for hiding this comment

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

comment here about why, pointing back to the issue/pr?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks - added.

Copy link
Contributor

Choose a reason for hiding this comment

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

we should strive to move this code inside internal (separate PR for sure)

index = Index(range(rows))
mgr.set_axis(1, index)
result = self.obj._constructor(mgr)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/groupby/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
)


@pytest.fixture(params=[True, False])
def as_index(request):
return request.param


@pytest.fixture
def mframe():
index = MultiIndex(
Expand Down
23 changes: 13 additions & 10 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Grouper,
Index,
MultiIndex,
RangeIndex,
Series,
Timestamp,
date_range,
Expand Down Expand Up @@ -2360,19 +2361,21 @@ def test_groupby_all_nan_groups_drop():
tm.assert_series_equal(result, expected)


def test_groupby_empty_multi_column():
# GH 15106
@pytest.mark.parametrize("numeric_only", [True, False])
def test_groupby_empty_multi_column(as_index, numeric_only):
# GH 15106 & GH 41998
df = DataFrame(data=[], columns=["A", "B", "C"])
gb = df.groupby(["A", "B"])
result = gb.sum(numeric_only=False)
expected = DataFrame(
[], columns=["C"], index=MultiIndex([[], []], [[], []], names=["A", "B"])
)
gb = df.groupby(["A", "B"], as_index=as_index)
result = gb.sum(numeric_only=numeric_only)
if as_index:
index = MultiIndex([[], []], [[], []], names=["A", "B"])
columns = ["C"] if not numeric_only else []
else:
index = RangeIndex(0)
columns = ["A", "B", "C"] if not numeric_only else ["A", "B"]
expected = DataFrame([], columns=columns, index=index)
tm.assert_frame_equal(result, expected)

result = gb.sum(numeric_only=True)
tm.assert_frame_equal(result, expected[[]])


def test_groupby_filtered_df_std():
# GH 16174
Expand Down