Skip to content

Fix DataFrame.to_string() justification (2) #22505

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 7 commits into from
Sep 25, 2018
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.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ I/O
- :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`)
- :func:`read_sas()` will correctly parse sas7bdat files with data page types having also bit 7 set (so page type is 128 + 256 = 384) (:issue:`16615`)
- Bug in :meth:`detect_client_encoding` where potential ``IOError`` goes unhandled when importing in a mod_wsgi process due to restricted access to stdout. (:issue:`21552`)
- Bug in :func:`to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`)

Plotting
^^^^^^^^
Expand Down
5 changes: 1 addition & 4 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,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).replace('\n ',
'\n').strip()
result = self.adj.adjoin(3, fmt_values)

if self.header and have_header:
result = fmt_index[0] + '\n' + result
Expand Down Expand Up @@ -650,8 +649,6 @@ 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:
Expand Down
32 changes: 28 additions & 4 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,18 +1269,42 @@ def test_to_string_specified_header(self):
df.to_string(header=['X'])

def test_to_string_no_index(self):
df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
# GH 16839, GH 13032
df = DataFrame({'x': [11, 22], 'y': [33, -44], 'z': ['AAA', ' ']})

df_s = df.to_string(index=False)
expected = "x y\n1 4\n2 5\n3 6"
# Leading space is expected for positive numbers.
expected = (" x y z\n"
" 11 33 AAA\n"
" 22 -44 ")
assert df_s == expected

df_s = df[['y', 'x', 'z']].to_string(index=False)
expected = (" y x z\n"
" 33 11 AAA\n"
"-44 22 ")
assert df_s == expected

def test_to_string_line_width_no_index(self):
# GH 13998, GH 22505
df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})

df_s = df.to_string(line_width=1, index=False)
expected = "x \\\n1 \n2 \n3 \n\ny \n4 \n5 \n6"
expected = " x \\\n 1 \n 2 \n 3 \n\n y \n 4 \n 5 \n 6 "

assert df_s == expected

df = DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]})

df_s = df.to_string(line_width=1, index=False)
expected = " x \\\n 11 \n 22 \n 33 \n\n y \n 4 \n 5 \n 6 "

assert df_s == expected

df = DataFrame({'x': [11, 22, -33], 'y': [4, 5, -6]})

df_s = df.to_string(line_width=1, index=False)
expected = " x \\\n 11 \n 22 \n-33 \n\n y \n 4 \n 5 \n-6 "

assert df_s == expected

Expand Down Expand Up @@ -1793,7 +1817,7 @@ 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')
assert result == expected

def test_unicode_name_in_footer(self):
Expand Down