Skip to content

Commit b12de52

Browse files
committed
Merge branch 'master' of https://github.com/MichaelWS/pandas into MichaelWS-master
2 parents 9d9b6dd + 66b9e8c commit b12de52

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Improvements to existing features
7272
- df.info() view now display dtype info per column (:issue: `5682`)
7373
- perf improvements in DataFrame ``count/dropna`` for ``axis=1``
7474
- Series.str.contains now has a `regex=False` keyword which can be faster for plain (non-regex) string patterns. (:issue: `5879`)
75+
- DataFrame.head(0) returns self instead of empty frame (:issue:`5846`)
7576

7677
Bug Fixes
7778
~~~~~~~~~
@@ -91,6 +92,7 @@ Bug Fixes
9192
- Bug in ``BusinessDay`` when adding n days to a date not on offset when n>5 and n%5==0 (:issue:`5890`)
9293
- Bug in assigning to chained series with a series via ix (:issue:`5928`)
9394
- Bug in creating an empty DataFrame, copying, then assigning (:issue:`5932`)
95+
- Bug in DataFrame.tail with empty frame (:issue:`5846`)
9496

9597
pandas 0.13.0
9698
-------------

pandas/core/generic.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,17 +1570,25 @@ def head(self, n=5):
15701570
Returns first n rows
15711571
"""
15721572
l = len(self)
1573-
if abs(n) > l:
1574-
n = l if n > 0 else -l
1573+
if l == 0 or n==0:
1574+
return self
1575+
if n > l:
1576+
n = l
1577+
elif n < -l:
1578+
n = -l
15751579
return self.iloc[:n]
15761580

15771581
def tail(self, n=5):
15781582
"""
15791583
Returns last n rows
15801584
"""
15811585
l = len(self)
1582-
if abs(n) > l:
1583-
n = l if n > 0 else -l
1586+
if l == 0 or n == 0:
1587+
return self
1588+
if n > l:
1589+
n = l
1590+
elif n < -l:
1591+
n = -l
15841592
return self.iloc[-n:]
15851593

15861594
#----------------------------------------------------------------------

pandas/tests/test_frame.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4259,12 +4259,25 @@ def test_repr_column_name_unicode_truncation_bug(self):
42594259
def test_head_tail(self):
42604260
assert_frame_equal(self.frame.head(), self.frame[:5])
42614261
assert_frame_equal(self.frame.tail(), self.frame[-5:])
4262-
4262+
assert_frame_equal(self.frame.head(0), self.frame)
4263+
assert_frame_equal(self.frame.tail(0), self.frame)
4264+
assert_frame_equal(self.frame.head(-1), self.frame[:-1])
4265+
assert_frame_equal(self.frame.tail(-1), self.frame[1:])
4266+
assert_frame_equal(self.frame.head(1), self.frame[:1])
4267+
assert_frame_equal(self.frame.tail(1), self.frame[-1:])
42634268
# with a float index
42644269
df = self.frame.copy()
42654270
df.index = np.arange(len(self.frame)) + 0.1
42664271
assert_frame_equal(df.head(), df.iloc[:5])
42674272
assert_frame_equal(df.tail(), df.iloc[-5:])
4273+
assert_frame_equal(df.head(0), df)
4274+
assert_frame_equal(df.tail(0), df)
4275+
assert_frame_equal(df.head(-1), df.iloc[:-1])
4276+
assert_frame_equal(df.tail(-1), df.iloc[1:])
4277+
#test empty dataframe
4278+
empty_df = DataFrame()
4279+
assert_frame_equal(empty_df.tail(), empty_df)
4280+
assert_frame_equal(empty_df.head(), empty_df)
42684281

42694282
def test_insert(self):
42704283
df = DataFrame(np.random.randn(5, 3), index=np.arange(5),

pandas/tests/test_generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def test_head_tail(self):
338338
self._compare(o.tail(), o.iloc[-5:])
339339

340340
# 0-len
341-
self._compare(o.head(0), o.iloc[:0])
341+
self._compare(o.head(0), o.iloc[:])
342342
self._compare(o.tail(0), o.iloc[0:])
343343

344344
# bounded

0 commit comments

Comments
 (0)