From cb97503559772dfae68b19b613684711844ae85a Mon Sep 17 00:00:00 2001 From: "surveymedia.ca" Date: Thu, 31 Dec 2015 15:26:02 -0500 Subject: [PATCH 1/2] head(0) and tail(0) return empty DataFrames unit test for Series.head(0), Series.tail(0) fixed breaking unit tests on Series.head(0)/Series.tail(0) removed unnecessary length checks added documentation mention series in API docs --- doc/source/whatsnew/v0.18.0.txt | 2 ++ pandas/core/generic.py | 8 ++------ pandas/tests/test_frame.py | 10 ++++++---- pandas/tests/test_generic.py | 4 ++-- pandas/tests/test_series.py | 3 ++- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 58d003b5c9dc7..2c97cae80ae2a 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -166,6 +166,8 @@ Backwards incompatible API changes - The parameter ``out`` has been removed from the ``Series.round()`` method. (:issue:`11763`) - ``DataFrame.round()`` leaves non-numeric columns unchanged in its return, rather than raises. (:issue:`11885`) +- ``DataFrame.head(0)`` and ``DataFrame.tail(0)`` return empty frames, rather than ``self``. (:issue:`11937`) +- ``Series.head(0)`` and ``Series.tail(0)`` return empty series, rather than ``self``. (:issue:`11937`) NaT and Timedelta operations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 85f23b988778f..958571fdc2218 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2136,18 +2136,14 @@ def head(self, n=5): """ Returns first n rows """ - l = len(self) - if l == 0 or n==0: - return self return self.iloc[:n] def tail(self, n=5): """ Returns last n rows """ - l = len(self) - if l == 0 or n == 0: - return self + if n == 0: + return self.iloc[0:0] return self.iloc[-n:] diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 57e75e3393b1b..d17df54d25ddd 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -5457,8 +5457,10 @@ def test_repr_column_name_unicode_truncation_bug(self): def test_head_tail(self): assert_frame_equal(self.frame.head(), self.frame[:5]) assert_frame_equal(self.frame.tail(), self.frame[-5:]) - assert_frame_equal(self.frame.head(0), self.frame) - assert_frame_equal(self.frame.tail(0), self.frame) + + assert_frame_equal(self.frame.head(0), self.frame[0:0]) + assert_frame_equal(self.frame.tail(0), self.frame[0:0]) + assert_frame_equal(self.frame.head(-1), self.frame[:-1]) assert_frame_equal(self.frame.tail(-1), self.frame[1:]) assert_frame_equal(self.frame.head(1), self.frame[:1]) @@ -5468,8 +5470,8 @@ def test_head_tail(self): df.index = np.arange(len(self.frame)) + 0.1 assert_frame_equal(df.head(), df.iloc[:5]) assert_frame_equal(df.tail(), df.iloc[-5:]) - assert_frame_equal(df.head(0), df) - assert_frame_equal(df.tail(0), df) + assert_frame_equal(df.head(0), df[0:0]) + assert_frame_equal(df.tail(0), df[0:0]) assert_frame_equal(df.head(-1), df.iloc[:-1]) assert_frame_equal(df.tail(-1), df.iloc[1:]) #test empty dataframe diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 478c65892173d..37cb38454f74e 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -342,8 +342,8 @@ def test_head_tail(self): self._compare(o.tail(), o.iloc[-5:]) # 0-len - self._compare(o.head(0), o.iloc[:]) - self._compare(o.tail(0), o.iloc[0:]) + self._compare(o.head(0), o.iloc[0:0]) + self._compare(o.tail(0), o.iloc[0:0]) # bounded self._compare(o.head(len(o)+1), o) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index ea9ee8fc5b235..ad79406c70704 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -7642,8 +7642,9 @@ def test_sortlevel(self): def test_head_tail(self): assert_series_equal(self.series.head(), self.series[:5]) + assert_series_equal(self.series.head(0), self.series[0:0]) assert_series_equal(self.series.tail(), self.series[-5:]) - + assert_series_equal(self.series.tail(0), self.series[0:0]) def test_isin(self): s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C']) From 2e20e60c3bd2a1208dccd256b70f828c2b524070 Mon Sep 17 00:00:00 2001 From: kaustuv deolal Date: Sat, 2 Jan 2016 15:09:12 +0530 Subject: [PATCH 2/2] issue-11833 solved , docs updated as well modifies test_format squashed --- doc/source/whatsnew/v0.18.0.txt | 20 ++++++++++++++++++++ pandas/core/format.py | 5 +++-- pandas/tests/test_format.py | 10 +++++----- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 2c97cae80ae2a..1425f7f68f3af 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -161,6 +161,26 @@ In addition, ``.round()`` will be available thru the ``.dt`` accessor of ``Serie .. _whatsnew_0180.api_breaking: +- the leading whitespaces have been removed from the output of ``to_string(index=False)`` method (:issue:'11833') + +Previous Behaviour for the DataFrame Objects + +.. code-block:: python + + In [3]: df = pd.DataFrame({'a':range(5),'b':range(5)}) + In [3]: df.to_string(index=False) + Out [4]: u' a b\n 0 0\n 1 1\n 2 2\n 3 3\n 4 4' + + +New Behaviour for the DataFrame Objects + +.. ipython:: python + + df=pd.DataFrame({'a':range(5),'b':range(5)}) + df.to_string(index=False) + +Similar behaviour applies for the Series objects as well. + Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pandas/core/format.py b/pandas/core/format.py index 91ac6f11f4ae9..c5452a57c516c 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -245,7 +245,7 @@ def to_string(self): if self.index: result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values]) else: - result = self.adj.adjoin(3, fmt_values) + result = self.adj.adjoin(3, fmt_values).replace('\n ','\n').strip() if self.header and have_header: result = fmt_index[0] + '\n' + result @@ -577,7 +577,8 @@ def to_string(self): self._chk_truncate() strcols = self._to_str_columns() text = self.adj.adjoin(1, *strcols) - + if not self.index: + text=text.replace('\n ','\n').strip() self.buf.writelines(text) if self.should_show_dimensions: diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 6c4e4dd844fc9..3526989c8c6e2 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -1725,7 +1725,7 @@ def test_to_string_no_index(self): 'y': [4, 5, 6]}) df_s = df.to_string(index=False) - expected = " x y\n 1 4\n 2 5\n 3 6" + expected = "x y\n1 4\n2 5\n3 6" self.assertEqual(df_s, expected) @@ -3109,10 +3109,10 @@ def test_to_string_without_index(self): #GH 11729 Test index=False option s= Series([1, 2, 3, 4]) result = s.to_string(index=False) - expected = (u(' 1\n') + - ' 2\n' + - ' 3\n' + - ' 4') + expected = (u('1\n') + + '2\n' + + '3\n' + + '4') self.assertEqual(result, expected) def test_unicode_name_in_footer(self):