Skip to content

DOC: DataFrameGroupBy.filter documentation is misleading #61300

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

Closed
1 task done
adamreeve opened this issue Apr 17, 2025 · 1 comment
Closed
1 task done

DOC: DataFrameGroupBy.filter documentation is misleading #61300

adamreeve opened this issue Apr 17, 2025 · 1 comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@adamreeve
Copy link

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.DataFrameGroupBy.filter.html and https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.SeriesGroupBy.filter.html

Documentation problem

Both DataFrameGroupBy.filter and SeriesGroupBy.filter state that they "filter elements from groups".

This is not true, these methods filter whole groups. If you attempt to filter individual elements within a group by returning a series of boolean you get an error:

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                          'foo', 'bar'],
                   'B' : [1, 2, 3, 4, 5, 6],
                  'C' : [2.0, 5., 8., 1., 2., 9.]})
df.groupby("A").filter(lambda x: x['B'] > 1).sum()
TypeError: filter function returned a Series, but expected a scalar bool

Suggested fix for documentation

Suggested documentation:

Filter groups that don’t satisfy a criterion.

Groups are filtered if they do not satisfy the boolean criterion specified by func.
@adamreeve adamreeve added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 17, 2025
@adamreeve
Copy link
Author

After looking at this again, I think the documentation does make sense but I was misinterpreting it. This does filter out rows/elements rather than whole groups, but it filters rows based on a criterion that applies to the whole group rather than to rows within the group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant