Skip to content

Commit 819a0a0

Browse files
BUG: fix empty groupby(..., as_index=False).apply(...) index name inconsistency (#65332)
1 parent a55fe00 commit 819a0a0

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

doc/source/whatsnew/v3.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ Groupby/resample/rolling
340340
^^^^^^^^^^^^^^^^^^^^^^^^
341341
- Bug in :meth:`.DataFrameGroupBy.agg` when there are no groups, multiple keys, and ``group_keys=False`` (:issue:`51445`)
342342
- Bug in :meth:`.DataFrameGroupBy.agg` would operate on the group as a whole when ``args`` or ``kwargs`` are supplied for the provided ``func``; now this method only operates on each Series of the group (:issue:`39169`)
343+
- Bug in :meth:`.DataFrameGroupBy.apply` with ``as_index=False`` where applying on an empty :class:`DataFrame` returned inconsistent index metadata compared to non-empty results (:issue:`48135`)
343344
- Bug in :meth:`.GroupBy.any` and :meth:`.GroupBy.all` returning ``NaN`` with ``float64`` dtype for unobserved categorical groups on NumPy ``bool`` data instead of the boolean identity value with ``bool`` dtype (:issue:`65100`)
344345
- Bug in :meth:`.Rolling.skew` and :meth:`.Rolling.kurt` (and their :class:`GroupBy` counterparts) returning ``0.0`` and ``-3.0`` respectively for degenerate windows or groups; these now return ``NaN`` (:issue:`62864`)
345346
- Bug in :meth:`.Rolling.skew` and :meth:`.Rolling.kurt` returning ``NaN`` for low-variance windows (:issue:`62946`)

pandas/core/groupby/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,7 @@ def _wrap_applied_output(
23932393
if is_transform:
23942394
# GH#47787 see test_group_on_empty_multiindex
23952395
res_index = data.index
2396-
elif not self.group_keys:
2396+
elif not self.group_keys or not self.as_index:
23972397
res_index = None
23982398
else:
23992399
res_index = self._grouper.result_index

pandas/tests/groupby/test_apply.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,3 +1541,29 @@ def addstore(x):
15411541

15421542
tm.assert_frame_equal(store[0], expected_out_0)
15431543
tm.assert_frame_equal(store[1], expected_out_1)
1544+
1545+
1546+
@pytest.mark.parametrize("test_empty", [False, True])
1547+
def test_apply_as_index_false_empty_index_name(test_empty):
1548+
# https://github.com/pandas-dev/pandas/issues/48135
1549+
df = DataFrame({"A": [4, 4, 4], "B": [9, 9, 9]})
1550+
1551+
if test_empty:
1552+
df = df.iloc[:0]
1553+
1554+
gb = df.groupby("A", as_index=False)
1555+
result = gb.apply(lambda x: x)
1556+
1557+
if test_empty:
1558+
expected = DataFrame(
1559+
{"B": Series([], dtype="int64")},
1560+
index=Index([], dtype="int64"),
1561+
)
1562+
else:
1563+
expected = DataFrame(
1564+
{"B": [9, 9, 9]},
1565+
index=Index([0, 1, 2], dtype="int64"),
1566+
)
1567+
1568+
expected.index.name = None
1569+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)