Skip to content

Commit 30b942a

Browse files
gshibajreback
authored andcommitted
Fix DataFrame.to_string() justification (2) (#22505)
1 parent ea83ccc commit 30b942a

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ I/O
762762
- :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`)
763763
- :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`)
764764
- 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`)
765+
- 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`)
765766

766767
Plotting
767768
^^^^^^^^

pandas/io/formats/format.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ def to_string(self):
288288
if self.index:
289289
result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values])
290290
else:
291-
result = self.adj.adjoin(3, fmt_values).replace('\n ',
292-
'\n').strip()
291+
result = self.adj.adjoin(3, fmt_values)
293292

294293
if self.header and have_header:
295294
result = fmt_index[0] + '\n' + result
@@ -650,8 +649,6 @@ def to_string(self):
650649
self._chk_truncate()
651650
strcols = self._to_str_columns()
652651
text = self.adj.adjoin(1, *strcols)
653-
if not self.index:
654-
text = text.replace('\n ', '\n').strip()
655652
self.buf.writelines(text)
656653

657654
if self.should_show_dimensions:

pandas/tests/io/formats/test_format.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1269,18 +1269,42 @@ def test_to_string_specified_header(self):
12691269
df.to_string(header=['X'])
12701270

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

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

1282+
df_s = df[['y', 'x', 'z']].to_string(index=False)
1283+
expected = (" y x z\n"
1284+
" 33 11 AAA\n"
1285+
"-44 22 ")
12771286
assert df_s == expected
12781287

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

12821292
df_s = df.to_string(line_width=1, index=False)
1283-
expected = "x \\\n1 \n2 \n3 \n\ny \n4 \n5 \n6"
1293+
expected = " x \\\n 1 \n 2 \n 3 \n\n y \n 4 \n 5 \n 6 "
1294+
1295+
assert df_s == expected
1296+
1297+
df = DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]})
1298+
1299+
df_s = df.to_string(line_width=1, index=False)
1300+
expected = " x \\\n 11 \n 22 \n 33 \n\n y \n 4 \n 5 \n 6 "
1301+
1302+
assert df_s == expected
1303+
1304+
df = DataFrame({'x': [11, 22, -33], 'y': [4, 5, -6]})
1305+
1306+
df_s = df.to_string(line_width=1, index=False)
1307+
expected = " x \\\n 11 \n 22 \n-33 \n\n y \n 4 \n 5 \n-6 "
12841308

12851309
assert df_s == expected
12861310

@@ -1793,7 +1817,7 @@ def test_to_string_without_index(self):
17931817
# GH 11729 Test index=False option
17941818
s = Series([1, 2, 3, 4])
17951819
result = s.to_string(index=False)
1796-
expected = (u('1\n') + '2\n' + '3\n' + '4')
1820+
expected = (u(' 1\n') + ' 2\n' + ' 3\n' + ' 4')
17971821
assert result == expected
17981822

17991823
def test_unicode_name_in_footer(self):

0 commit comments

Comments
 (0)