From 7d7d210cc40206564d63e4b0d1239eef0e987173 Mon Sep 17 00:00:00 2001 From: Jeff Hammerbacher Date: Wed, 26 Oct 2011 07:48:19 -0700 Subject: [PATCH] ENH: add .head() and .tail() to Series --- pandas/core/series.py | 10 ++++++++++ pandas/tests/test_series.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 9015b5ee7d..6a1259ee27 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -553,6 +553,16 @@ def get(self, key, default=None): else: return default + def head(self, n=5): + """Returns first n rows of Series + """ + return self[:n] + + def tail(self, n=5): + """Returns last n rows of Series + """ + return self[-n:] + #---------------------------------------------------------------------- # Statistics, overridden ndarray methods diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 6c909f6d40..5098ad6e4f 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1244,6 +1244,10 @@ def test_unstack(self): unstacked = s.unstack(0) assert_frame_equal(unstacked, expected) + def test_head_tail(self): + assert_frame_equal(self.series.head(), self.series[:5]) + assert_frame_equal(self.series.tail(), self.series[-5:]) + #------------------------------------------------------------------------------- # TimeSeries-specific