Skip to content

Commit fd7db98

Browse files
bharatr21TomAugspurger
authored andcommitted
DOC: Document behaviour of head(n), tail(n) for negative values of n except on GroupBy (#30556)
* DOC: Document negative values on head(n), tail(n) except for GroupBy
1 parent 733eb77 commit fd7db98

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

pandas/core/generic.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -4652,14 +4652,17 @@ def head(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
46524652
on position. It is useful for quickly testing if your object
46534653
has the right type of data in it.
46544654
4655+
For negative values of `n`, this function returns all rows except
4656+
the last `n` rows, equivalent to ``df[:-n]``.
4657+
46554658
Parameters
46564659
----------
46574660
n : int, default 5
46584661
Number of rows to select.
46594662
46604663
Returns
46614664
-------
4662-
obj_head : same type as caller
4665+
same type as caller
46634666
The first `n` rows of the caller object.
46644667
46654668
See Also
@@ -4699,6 +4702,17 @@ def head(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
46994702
0 alligator
47004703
1 bee
47014704
2 falcon
4705+
4706+
For negative values of `n`
4707+
4708+
>>> df.head(-3)
4709+
animal
4710+
0 alligator
4711+
1 bee
4712+
2 falcon
4713+
3 lion
4714+
4 monkey
4715+
5 parrot
47024716
"""
47034717

47044718
return self.iloc[:n]
@@ -4711,6 +4725,9 @@ def tail(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
47114725
position. It is useful for quickly verifying data, for example,
47124726
after sorting or appending rows.
47134727
4728+
For negative values of `n`, this function returns all rows except
4729+
the first `n` rows, equivalent to ``df[n:]``.
4730+
47144731
Parameters
47154732
----------
47164733
n : int, default 5
@@ -4758,6 +4775,17 @@ def tail(self: FrameOrSeries, n: int = 5) -> FrameOrSeries:
47584775
6 shark
47594776
7 whale
47604777
8 zebra
4778+
4779+
For negative values of `n`
4780+
4781+
>>> df.tail(-3)
4782+
animal
4783+
3 lion
4784+
4 monkey
4785+
5 parrot
4786+
6 shark
4787+
7 whale
4788+
8 zebra
47614789
"""
47624790

47634791
if n == 0:

pandas/core/groupby/groupby.py

+12
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,8 @@ def head(self, n=5):
23772377
from the original DataFrame with original index and order preserved
23782378
(``as_index`` flag is ignored).
23792379
2380+
Does not work for negative values of `n`.
2381+
23802382
Returns
23812383
-------
23822384
Series or DataFrame
@@ -2390,6 +2392,10 @@ def head(self, n=5):
23902392
A B
23912393
0 1 2
23922394
2 5 6
2395+
>>> df.groupby('A').head(-1)
2396+
Empty DataFrame
2397+
Columns: [A, B]
2398+
Index: []
23932399
"""
23942400
self._reset_group_selection()
23952401
mask = self._cumcount_array() < n
@@ -2405,6 +2411,8 @@ def tail(self, n=5):
24052411
from the original DataFrame with original index and order preserved
24062412
(``as_index`` flag is ignored).
24072413
2414+
Does not work for negative values of `n`.
2415+
24082416
Returns
24092417
-------
24102418
Series or DataFrame
@@ -2418,6 +2426,10 @@ def tail(self, n=5):
24182426
A B
24192427
1 a 2
24202428
3 b 2
2429+
>>> df.groupby('A').tail(-1)
2430+
Empty DataFrame
2431+
Columns: [A, B]
2432+
Index: []
24212433
"""
24222434
self._reset_group_selection()
24232435
mask = self._cumcount_array(ascending=False) < n

0 commit comments

Comments
 (0)