Skip to content

Commit 859a2a3

Browse files
dontgotopmhatre1
authored andcommitted
BUG: pandas-dev#57775 Fix groupby apply in case func returns None for all groups (pandas-dev#57800)
* Ensure that the empty frame has the information of the original frame * Adjust test to expect DataFrame with columns * Construct leaner dataframe * Update doc * Add example to doc * Update whatsnew * Add issue #; phrasing * Fix doc * Fix doc * Fix docstring formatting * move from 2.2.2 to 3.0.0 * remove description * fix whitespace
1 parent 0e147aa commit 859a2a3

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

pandas/core/groupby/generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1642,8 +1642,11 @@ def _wrap_applied_output(
16421642
first_not_none = next(com.not_none(*values), None)
16431643

16441644
if first_not_none is None:
1645-
# GH9684 - All values are None, return an empty frame.
1646-
return self.obj._constructor()
1645+
# GH9684 - All values are None, return an empty frame
1646+
# GH57775 - Ensure that columns and dtypes from original frame are kept.
1647+
result = self.obj._constructor(columns=data.columns)
1648+
result = result.astype(data.dtypes)
1649+
return result
16471650
elif isinstance(first_not_none, DataFrame):
16481651
return self._concat_objects(
16491652
values,

pandas/core/groupby/groupby.py

+8
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,14 @@ def apply(self, func, *args, include_groups: bool = True, **kwargs) -> NDFrameT:
16361636
a 5
16371637
b 2
16381638
dtype: int64
1639+
1640+
Example 4: The function passed to ``apply`` returns ``None`` for one of the
1641+
group. This group is filtered from the result:
1642+
1643+
>>> g1.apply(lambda x: None if x.iloc[0, 0] == 3 else x, include_groups=False)
1644+
B C
1645+
0 1 4
1646+
1 2 6
16391647
"""
16401648
if isinstance(func, str):
16411649
if hasattr(self, func):

pandas/tests/groupby/test_apply.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,8 @@ def test_func(x):
838838
msg = "DataFrameGroupBy.apply operated on the grouping columns"
839839
with tm.assert_produces_warning(DeprecationWarning, match=msg):
840840
result = test_df.groupby("groups").apply(test_func)
841-
expected = DataFrame()
841+
expected = DataFrame(columns=test_df.columns)
842+
expected = expected.astype(test_df.dtypes)
842843
tm.assert_frame_equal(result, expected)
843844

844845

0 commit comments

Comments
 (0)