Skip to content

Commit 4edd862

Browse files
committed
Merge pull request #5373 from jreback/float_index
BUG: Make sure that head/tail are iloc based, (GH5370)
2 parents ea97682 + c6d07a8 commit 4edd862

File tree

7 files changed

+64
-20
lines changed

7 files changed

+64
-20
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ Bug Fixes
756756
- Test suite no longer leaves around temporary files when testing graphics. (:issue:`5347`)
757757
(thanks for catching this @yarikoptic!)
758758
- Fixed html tests on win32. (:issue:`4580`)
759+
- Make sure that ``head/tail`` are ``iloc`` based, (:issue:`5370`)
759760

760761

761762
pandas 0.12.0

pandas/core/frame.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,16 +3085,6 @@ def last_valid_index(self):
30853085
"""
30863086
return self.index[self.count(1) > 0][-1]
30873087

3088-
def head(self, n=5):
3089-
"""Returns first n rows of DataFrame
3090-
"""
3091-
return self[:n]
3092-
3093-
def tail(self, n=5):
3094-
"""Returns last n rows of DataFrame
3095-
"""
3096-
return self[-n:]
3097-
30983088
#----------------------------------------------------------------------
30993089
# Data reshaping
31003090

pandas/core/generic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,24 @@ def filter(self, items=None, like=None, regex=None, axis=None):
14611461
else:
14621462
raise TypeError('Must pass either `items`, `like`, or `regex`')
14631463

1464+
def head(self, n=5):
1465+
"""
1466+
Returns first n rows
1467+
"""
1468+
l = len(self)
1469+
if abs(n) > l:
1470+
n = l if n > 0 else -l
1471+
return self.iloc[:n]
1472+
1473+
def tail(self, n=5):
1474+
"""
1475+
Returns last n rows
1476+
"""
1477+
l = len(self)
1478+
if abs(n) > l:
1479+
n = l if n > 0 else -l
1480+
return self.iloc[-n:]
1481+
14641482
#----------------------------------------------------------------------
14651483
# Attribute access
14661484

pandas/core/panel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ def conform(self, frame, axis='items'):
581581
axes = self._get_plane_axes(axis)
582582
return frame.reindex(**self._extract_axes_for_slice(self, axes))
583583

584+
def head(self, n=5):
585+
raise NotImplementedError
586+
587+
def tail(self, n=5):
588+
raise NotImplementedError
589+
584590
def _needs_reindex_multi(self, axes, method, level):
585591
# only allowing multi-index on Panel (and not > dims)
586592
return method is None and not self._is_mixed_type and self._AXIS_LEN <= 3 and com._count_not_none(*axes.values()) == 3

pandas/core/series.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,16 +1058,6 @@ def to_sparse(self, kind='block', fill_value=None):
10581058
return SparseSeries(self, kind=kind,
10591059
fill_value=fill_value).__finalize__(self)
10601060

1061-
def head(self, n=5):
1062-
"""Returns first n rows of Series
1063-
"""
1064-
return self[:n]
1065-
1066-
def tail(self, n=5):
1067-
"""Returns last n rows of Series
1068-
"""
1069-
return self[-n:]
1070-
10711061
#----------------------------------------------------------------------
10721062
# Statistics, overridden ndarray methods
10731063

pandas/tests/test_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,12 @@ def test_head_tail(self):
42614261
assert_frame_equal(self.frame.head(), self.frame[:5])
42624262
assert_frame_equal(self.frame.tail(), self.frame[-5:])
42634263

4264+
# with a float index
4265+
df = self.frame.copy()
4266+
df.index = np.arange(len(self.frame)) + 0.1
4267+
assert_frame_equal(df.head(), df.iloc[:5])
4268+
assert_frame_equal(df.tail(), df.iloc[-5:])
4269+
42644270
def test_insert(self):
42654271
df = DataFrame(np.random.randn(5, 3), index=np.arange(5),
42664272
columns=['c', 'b', 'a'])

pandas/tests/test_generic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,39 @@ def test_metadata_propagation(self):
317317
except (ValueError):
318318
pass
319319

320+
def test_head_tail(self):
321+
# GH5370
322+
323+
o = self._construct(shape=10)
324+
325+
# check all index types
326+
for index in [ tm.makeFloatIndex, tm.makeIntIndex,
327+
tm.makeStringIndex, tm.makeUnicodeIndex,
328+
tm.makeDateIndex, tm.makePeriodIndex ]:
329+
axis = o._get_axis_name(0)
330+
setattr(o,axis,index(len(getattr(o,axis))))
331+
332+
# Panel + dims
333+
try:
334+
o.head()
335+
except (NotImplementedError):
336+
raise nose.SkipTest('not implemented on {0}'.format(o.__class__.__name__))
337+
338+
self._compare(o.head(), o.iloc[:5])
339+
self._compare(o.tail(), o.iloc[-5:])
340+
341+
# 0-len
342+
self._compare(o.head(0), o.iloc[:0])
343+
self._compare(o.tail(0), o.iloc[0:])
344+
345+
# bounded
346+
self._compare(o.head(len(o)+1), o)
347+
self._compare(o.tail(len(o)+1), o)
348+
349+
# neg index
350+
self._compare(o.head(-3), o.head(7))
351+
self._compare(o.tail(-3), o.tail(7))
352+
320353
class TestSeries(unittest.TestCase, Generic):
321354
_typ = Series
322355
_comparator = lambda self, x, y: assert_series_equal(x,y)

0 commit comments

Comments
 (0)