Skip to content

ENH : GH11729 added index parameter in series to_string #11750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really what dataframe does ? (e.g. with the adjoin)? for the no index case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any thoughts here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dataframe does not use adjoin. In dataframe we use a list and insert index into that. I was expecting that dataframe and series formatter code will be similar but this is not the case .

result = self.adj.adjoin(3, fmt_values)

if self.header and have_header:
result = fmt_index[0] + '\n' + result
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche does this look right? (e.g. I know we add a space, but seems odd with no index....)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems more logical to me that the space is not there if no index is added.

But, DataFrame.to_string also does this:

In [13]: print df.to_string()
   a  b  c
0  1  b  1
1  1  b  2
2  2  b  3
3  2  a  4

In [14]: print df.to_string(index=False)
 a  b  c
 1  b  1
 1  b  2
 2  b  3
 2  a  4

Of course not necessarily a reason to do it here as well

' 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'))
Expand Down