Skip to content

Commit bd996c5

Browse files
committed
Merge branch 'master' of github.com:pydata/pandas
* 'master' of github.com:pydata/pandas: BUG: take into account adjoin width, closes #3201
2 parents 9496c4a + 3b7f635 commit bd996c5

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pandas/core/format.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,11 @@ def to_string(self, force_unicode=None):
317317

318318
def _join_multiline(self, *strcols):
319319
lwidth = self.line_width
320+
adjoin_width = 1
320321
strcols = list(strcols)
321322
if self.index:
322323
idx = strcols.pop(0)
323-
lwidth -= np.array([len(x) for x in idx]).max()
324+
lwidth -= np.array([len(x) for x in idx]).max() + adjoin_width
324325

325326
col_widths = [np.array([len(x) for x in col]).max()
326327
if len(col) > 0 else 0
@@ -339,7 +340,7 @@ def _join_multiline(self, *strcols):
339340
else:
340341
row.append([' '] * len(self.frame))
341342

342-
str_lst.append(adjoin(1, *row))
343+
str_lst.append(adjoin(adjoin_width, *row))
343344
st = ed
344345
return '\n\n'.join(str_lst)
345346

@@ -1765,14 +1766,21 @@ def _put_lines(buf, lines):
17651766
buf.write('\n'.join(lines))
17661767

17671768

1768-
def _binify(cols, width):
1769+
def _binify(cols, line_width):
1770+
adjoin_width = 1
17691771
bins = []
17701772
curr_width = 0
1773+
i_last_column = len(cols) - 1
17711774
for i, w in enumerate(cols):
1772-
curr_width += w
1773-
if curr_width + 2 > width and i > 0:
1775+
w_adjoined = w + adjoin_width
1776+
curr_width += w_adjoined
1777+
if i_last_column == i:
1778+
wrap = curr_width + 1 > line_width and i > 0
1779+
else:
1780+
wrap = curr_width + 2 > line_width and i > 0
1781+
if wrap:
17741782
bins.append(i)
1775-
curr_width = w
1783+
curr_width = w_adjoined
17761784

17771785
bins.append(len(cols))
17781786
return bins

pandas/tests/test_format.py

+5
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,11 @@ def test_to_string_format_na(self):
909909
'4 4 bar')
910910
self.assertEqual(result, expected)
911911

912+
def test_to_string_line_width(self):
913+
df = pd.DataFrame(123, range(10, 15), range(30))
914+
s = df.to_string(line_width=80)
915+
self.assertEqual(max(len(l) for l in s.split('\n')), 80)
916+
912917
def test_to_html(self):
913918
# big mixed
914919
biggie = DataFrame({'A': randn(200),

0 commit comments

Comments
 (0)