Skip to content

Commit a1e7d53

Browse files
committed
Merge pull request #11937 from trvrm/master
head(0) and tail(0) return empty DataFrames
2 parents c41b458 + 7f00dbc commit a1e7d53

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

doc/source/whatsnew/v0.18.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ Backwards incompatible API changes
166166

167167
- The parameter ``out`` has been removed from the ``Series.round()`` method. (:issue:`11763`)
168168
- ``DataFrame.round()`` leaves non-numeric columns unchanged in its return, rather than raises. (:issue:`11885`)
169+
- ``DataFrame.head(0)`` and ``DataFrame.tail(0)`` return empty frames, rather than ``self``. (:issue:`11937`)
170+
- ``Series.head(0)`` and ``Series.tail(0)`` return empty series, rather than ``self``. (:issue:`11937`)
169171

170172
NaT and Timedelta operations
171173
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pandas/core/generic.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -2136,18 +2136,14 @@ def head(self, n=5):
21362136
"""
21372137
Returns first n rows
21382138
"""
2139-
l = len(self)
2140-
if l == 0 or n==0:
2141-
return self
21422139
return self.iloc[:n]
21432140

21442141
def tail(self, n=5):
21452142
"""
21462143
Returns last n rows
21472144
"""
2148-
l = len(self)
2149-
if l == 0 or n == 0:
2150-
return self
2145+
if n == 0:
2146+
return self.iloc[0:0]
21512147
return self.iloc[-n:]
21522148

21532149

pandas/tests/test_frame.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5457,8 +5457,10 @@ def test_repr_column_name_unicode_truncation_bug(self):
54575457
def test_head_tail(self):
54585458
assert_frame_equal(self.frame.head(), self.frame[:5])
54595459
assert_frame_equal(self.frame.tail(), self.frame[-5:])
5460-
assert_frame_equal(self.frame.head(0), self.frame)
5461-
assert_frame_equal(self.frame.tail(0), self.frame)
5460+
5461+
assert_frame_equal(self.frame.head(0), self.frame[0:0])
5462+
assert_frame_equal(self.frame.tail(0), self.frame[0:0])
5463+
54625464
assert_frame_equal(self.frame.head(-1), self.frame[:-1])
54635465
assert_frame_equal(self.frame.tail(-1), self.frame[1:])
54645466
assert_frame_equal(self.frame.head(1), self.frame[:1])
@@ -5468,8 +5470,8 @@ def test_head_tail(self):
54685470
df.index = np.arange(len(self.frame)) + 0.1
54695471
assert_frame_equal(df.head(), df.iloc[:5])
54705472
assert_frame_equal(df.tail(), df.iloc[-5:])
5471-
assert_frame_equal(df.head(0), df)
5472-
assert_frame_equal(df.tail(0), df)
5473+
assert_frame_equal(df.head(0), df[0:0])
5474+
assert_frame_equal(df.tail(0), df[0:0])
54735475
assert_frame_equal(df.head(-1), df.iloc[:-1])
54745476
assert_frame_equal(df.tail(-1), df.iloc[1:])
54755477
#test empty dataframe

pandas/tests/test_generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ def test_head_tail(self):
342342
self._compare(o.tail(), o.iloc[-5:])
343343

344344
# 0-len
345-
self._compare(o.head(0), o.iloc[:])
346-
self._compare(o.tail(0), o.iloc[0:])
345+
self._compare(o.head(0), o.iloc[0:0])
346+
self._compare(o.tail(0), o.iloc[0:0])
347347

348348
# bounded
349349
self._compare(o.head(len(o)+1), o)

pandas/tests/test_series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7642,8 +7642,9 @@ def test_sortlevel(self):
76427642

76437643
def test_head_tail(self):
76447644
assert_series_equal(self.series.head(), self.series[:5])
7645+
assert_series_equal(self.series.head(0), self.series[0:0])
76457646
assert_series_equal(self.series.tail(), self.series[-5:])
7646-
7647+
assert_series_equal(self.series.tail(0), self.series[0:0])
76477648
def test_isin(self):
76487649
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])
76497650

0 commit comments

Comments
 (0)