-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: #57775 Fix groupby apply in case func returns None for all groups #57800
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
Changes from 10 commits
b30a1d4
9510d52
ecd8b20
f3c1548
d0e2914
72e56c4
def8581
25e4a73
c26bfbc
7a033bc
da9e24a
6cb47ca
146697f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ Fixed regressions | |
|
||
Bug fixes | ||
~~~~~~~~~ | ||
- | ||
- :meth:`DataFrameGroupBy.apply` was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you undo this white space change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks, I missed that one. |
||
.. --------------------------------------------------------------------------- | ||
.. _whatsnew_222.other: | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1572,6 +1572,13 @@ def apply(self, func, *args, include_groups: bool = True, **kwargs) -> NDFrameT: | |||||||||||||
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation` | ||||||||||||||
for more details. | ||||||||||||||
|
||||||||||||||
Groups for which ``func`` returns ``None`` will be filtered from the result. | ||||||||||||||
|
||||||||||||||
.. versionchanged:: 2.2.2 | ||||||||||||||
|
||||||||||||||
In case all groups are filtered from the result, an empty DataFrame | ||||||||||||||
with the columns and dtypes of the original dataframe will be returned. | ||||||||||||||
|
||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(the example below is sufficient) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the description, only left the example |
||||||||||||||
Examples | ||||||||||||||
-------- | ||||||||||||||
>>> df = pd.DataFrame({"A": "a a b".split(), "B": [1, 2, 3], "C": [4, 6, 5]}) | ||||||||||||||
|
@@ -1636,6 +1643,14 @@ def apply(self, func, *args, include_groups: bool = True, **kwargs) -> NDFrameT: | |||||||||||||
a 5 | ||||||||||||||
b 2 | ||||||||||||||
dtype: int64 | ||||||||||||||
|
||||||||||||||
Example 4: The function passed to ``apply`` returns ``None`` for one of the | ||||||||||||||
group. This group is filtered from the result: | ||||||||||||||
|
||||||||||||||
>>> g1.apply(lambda x: None if x.iloc[0, 0] == 3 else x, include_groups=False) | ||||||||||||||
B C | ||||||||||||||
0 1 4 | ||||||||||||||
1 2 6 | ||||||||||||||
""" | ||||||||||||||
if isinstance(func, str): | ||||||||||||||
if hasattr(self, func): | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to
v3.0.0.rst
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done