Skip to content

DOC: Document behaviour of head(n), tail(n) for negative values of n except on GroupBy #30556

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
merged 3 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4652,14 +4652,17 @@ def head(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
on position. It is useful for quickly testing if your object
has the right type of data in it.

For negative values of `n`, this function returns all rows except
the last `n` rows, equivalent to ``df[:-n]``.

Parameters
----------
n : int, default 5
Number of rows to select.

Returns
-------
obj_head : same type as caller
same type as caller
The first `n` rows of the caller object.

See Also
Expand Down Expand Up @@ -4699,6 +4702,17 @@ def head(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
0 alligator
1 bee
2 falcon

For negative values of `n`

>>> df.head(-3)
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
5 parrot
"""

return self.iloc[:n]
Expand All @@ -4711,6 +4725,9 @@ def tail(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
position. It is useful for quickly verifying data, for example,
after sorting or appending rows.

For negative values of `n`, this function returns all rows except
the first `n` rows, equivalent to ``df[n:]``.

Parameters
----------
n : int, default 5
Expand Down Expand Up @@ -4758,6 +4775,17 @@ def tail(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
6 shark
7 whale
8 zebra

For negative values of `n`

>>> df.tail(-3)
animal
3 lion
4 monkey
5 parrot
6 shark
7 whale
8 zebra
"""

if n == 0:
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,8 @@ def head(self, n=5):
from the original DataFrame with original index and order preserved
(``as_index`` flag is ignored).

Does not work for negative values of `n`.

Returns
-------
Series or DataFrame
Expand All @@ -2390,6 +2392,10 @@ def head(self, n=5):
A B
0 1 2
2 5 6
>>> df.groupby('A').head(-1)
Empty DataFrame
Columns: [A, B]
Index: []
"""
self._reset_group_selection()
mask = self._cumcount_array() < n
Expand All @@ -2405,6 +2411,8 @@ def tail(self, n=5):
from the original DataFrame with original index and order preserved
(``as_index`` flag is ignored).

Does not work for negative values of `n`.

Returns
-------
Series or DataFrame
Expand All @@ -2418,6 +2426,10 @@ def tail(self, n=5):
A B
1 a 2
3 b 2
>>> df.groupby('A').tail(-1)
Empty DataFrame
Columns: [A, B]
Index: []
"""
self._reset_group_selection()
mask = self._cumcount_array(ascending=False) < n
Expand Down