Skip to content

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

Merged

Conversation

dontgoto
Copy link
Contributor

@dontgoto dontgoto commented Mar 10, 2024

In case the func passed to DataFrameGroupBy.apply returns None for all groups (filtering out all groups), the original dataframe's columns and dtypes are now part of the empty result dataframe.

@dontgoto dontgoto requested a review from rhshadrach as a code owner March 10, 2024 20:37
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

seems reasonable to me. @rhshadrach

Comment on lines 1575 to 1581
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.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

(the example below is sufficient)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the description, only left the example

@@ -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`)
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -22,7 +22,6 @@ Fixed regressions
Bug fixes
~~~~~~~~~
-

Copy link
Member

Choose a reason for hiding this comment

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

Could you undo this white space change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks, I missed that one.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@rhshadrach rhshadrach added the Apply Apply, Aggregate, Transform, Map label Mar 12, 2024
@rhshadrach rhshadrach added this to the 3.0 milestone Mar 12, 2024
@mroeschke mroeschke merged commit 8130850 into pandas-dev:main Mar 12, 2024
51 checks passed
@mroeschke
Copy link
Member

Thanks @dontgoto

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Groupby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: groupby.apply when func always returns None returns empty dataframe
4 participants