Skip to content

Update groupby().first() documentation to clarify behavior with missing data (#27578) #61345

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ericcht
Copy link

@ericcht ericcht commented Apr 23, 2025

This PR enhances the docstring for GroupBy.first() to clarify:

  • It returns the first non-null value per column
  • It differs from .nth(0) and .head(1) in how it treats missing values
  • Includes comparative examples for better understanding

Fixes part of issue #27578

Ready for review.

@ericcht ericcht requested a review from rhshadrach as a code owner April 23, 2025 18:39
@ericcht ericcht force-pushed the doc-groupby-first-update branch from 4234958 to b4d068f Compare April 23, 2025 18:50
@ericcht ericcht marked this pull request as draft April 23, 2025 19:04
@ericcht ericcht closed this Apr 23, 2025
@ericcht ericcht deleted the doc-groupby-first-update branch April 23, 2025 19:07
@ericcht ericcht changed the title DOC: clarify column-wise behavior of GroupBy.first and its handling of nulls (#42406) Update groupby().first() documentation to clarify behavior with missing data (#27578) Apr 23, 2025
@ericcht ericcht restored the doc-groupby-first-update branch April 23, 2025 19:09
@ericcht ericcht reopened this Apr 23, 2025
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.

Thanks for the PR!

@@ -3232,9 +3232,12 @@ def first(
self, numeric_only: bool = False, min_count: int = -1, skipna: bool = True
) -> NDFrameT:
"""
Compute the first entry of each column within each group.
Compute the first non-null entry of each column within each group.
Copy link
Member

Choose a reason for hiding this comment

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

pandas documentation is quite consistent with using NA instead of null. Can you use NA throughout.

Copy link
Member

Choose a reason for hiding this comment

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

In addition, this line is incorrect as you can pass skipna=False.

core.groupby.DataFrameGroupBy.nth : Take the nth row from each group.
DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns.
Series.groupby : Group Series using a mapper or by a Series of values.
GroupBy.nth : Take the nth row from each group.
Copy link
Member

Choose a reason for hiding this comment

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

GroupBy is not public. Can you use DataFrameGroupBy instead.

Comment on lines +3285 to +3289
>>> df.groupby("A").nth(0)
B C D
A
1 NaN 1 2000-03-11
3 6.0 3 2000-03-13
Copy link
Member

Choose a reason for hiding this comment

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

I think this page should only include documentation on first. Can you remove the use of other methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants