Skip to content
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

DOC: Fix groupby nth #13810

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,32 +1205,55 @@ def nth(self, n, dropna=None):

Examples
--------
>>> df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])

>>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2],
... 'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B'])
>>> g = df.groupby('A')
>>> g.nth(0)
A B
0 1 NaN
2 5 6
B
A
1 NaN
2 3.0
>>> g.nth(1)
A B
1 1 4
B
A
1 2.0
2 5.0
>>> g.nth(-1)
A B
1 1 4
2 5 6
B
A
1 4.0
2 5.0
>>> g.nth([0, 1])
B
A
1 NaN
1 2.0
2 3.0
2 5.0

Specifying ``dropna`` allows count ignoring NaN

>>> g.nth(0, dropna='any')
B
A
1 4
5 6
B
A
1 2.0
2 3.0

NaNs denote group exhausted when using dropna

>>> g.nth(1, dropna='any')
>>> g.nth(3, dropna='any')
B
A
A
1 NaN
5 NaN
2 NaN

Specifying ``as_index=False`` in ``groupby`` keeps the original index.

>>> df.groupby('A', as_index=False).nth(1)
A B
1 1 2.0
4 2 5.0
"""

if isinstance(n, int):
Expand Down