From 6e2915979b1653f935251eeb78b8c96fa79c9b43 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 2 Dec 2015 23:45:27 -0500 Subject: [PATCH] added index parameter in series to_string --- doc/source/whatsnew/v0.18.0.txt | 1 + pandas/core/format.py | 10 +++++++--- pandas/core/series.py | 14 ++++++++------ pandas/tests/test_format.py | 10 ++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index aa43ebd70320f..b1b9c46293a91 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -32,6 +32,7 @@ Other enhancements ^^^^^^^^^^^^^^^^^^ - Handle truncated floats in SAS xport files (:issue:`11713`) +- Added option to hide index in ``Series.to_string`` (:issue:`11729`) - ``read_excel`` now supports s3 urls of the format ``s3://bucketname/filename`` (:issue:`11447`) .. _whatsnew_0180.enhancements.rounding: diff --git a/pandas/core/format.py b/pandas/core/format.py index 009e3877f0139..07f16a5ef480a 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -126,14 +126,15 @@ def to_string(self): class SeriesFormatter(object): def __init__(self, series, buf=None, length=True, header=True, - na_rep='NaN', name=False, float_format=None, dtype=True, - max_rows=None): + index=True, na_rep='NaN', name=False, float_format=None, + dtype=True, max_rows=None): self.series = series self.buf = buf if buf is not None else StringIO() self.name = name self.na_rep = na_rep self.header = header self.length = length + self.index = index self.max_rows = max_rows if float_format is None: @@ -241,7 +242,10 @@ def to_string(self): fmt_values.insert(row_num + n_header_rows, dot_str) fmt_index.insert(row_num + 1, '') - result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values]) + if self.index: + result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values]) + else: + result = self.adj.adjoin(3, fmt_values) if self.header and have_header: result = fmt_index[0] + '\n' + result diff --git a/pandas/core/series.py b/pandas/core/series.py index f3e059b3d6e98..8f8286aecfca3 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -959,7 +959,7 @@ def __unicode__(self): return result def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, - length=False, dtype=False, name=False, max_rows=None): + index=True, length=False, dtype=False, name=False, max_rows=None): """ Render a string representation of the Series @@ -974,6 +974,8 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, default None header: boolean, default True Add the Series header (index name) + index : bool, optional + Add index (row) labels, default True length : boolean, default False Add the Series length dtype : boolean, default False @@ -990,8 +992,8 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, """ the_repr = self._get_repr(float_format=float_format, na_rep=na_rep, - header=header, length=length, dtype=dtype, - name=name, max_rows=max_rows) + header=header, index=index, length=length, + dtype=dtype, name=name, max_rows=max_rows) # catch contract violations if not isinstance(the_repr, compat.text_type): @@ -1009,15 +1011,15 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, f.write(the_repr) def _get_repr( - self, name=False, header=True, length=True, dtype=True, na_rep='NaN', - float_format=None, max_rows=None): + self, name=False, header=True, index=True, length=True, dtype=True, + na_rep='NaN', float_format=None, max_rows=None): """ Internal function, should always return unicode string """ formatter = fmt.SeriesFormatter(self, name=name, length=length, header=header, - dtype=dtype, + index=index, dtype=dtype, na_rep=na_rep, float_format=float_format, max_rows=max_rows) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index c076ef0470d79..935c85ca3e29d 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -3181,6 +3181,16 @@ def test_to_string_float_na_spacing(self): '4 NaN') self.assertEqual(result, expected) + 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') + self.assertEqual(result, expected) + def test_unicode_name_in_footer(self): s = Series([1, 2], name=u('\u05e2\u05d1\u05e8\u05d9\u05ea')) sf = fmt.SeriesFormatter(s, name=u('\u05e2\u05d1\u05e8\u05d9\u05ea'))