Skip to content

Commit f7143cb

Browse files
committed
Merge remote branch 'y-p/fix_col_space'
* y-p/fix_col_space: DOC: change definition of col_space. BUG: df.to_html() should respect col_space argument. #1000 TST: df.to_html() should respect col_space argument BUG: df.to_string() should respect self.col_space, #1000 TST: df.to_string() should respect self.col_space
2 parents ef7c55e + efe48a5 commit f7143cb

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

doc/source/io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ over the string representation of the object. All arguments are optional:
669669

670670
- ``buf`` default None, for example a StringIO object
671671
- ``columns`` default None, which columns to write
672-
- ``col_space`` default None, number of spaces to write between columns
672+
- ``col_space`` default None, minimum width of each column.
673673
- ``na_rep`` default ``NaN``, representation of NA value
674674
- ``formatters`` default None, a dictionary (by column) of functions each of
675675
which takes a single argument and returns a formatted string

pandas/core/format.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
columns : sequence, optional
3131
the subset of columns to write; default None writes all columns
3232
col_space : int, optional
33-
the width of each columns
33+
the minimum width of each column
3434
header : bool, optional
3535
whether to print column labels, default True
3636
index : bool, optional
@@ -221,7 +221,7 @@ def _to_str_columns(self, force_unicode=None):
221221
fmt_values = self._format_col(i)
222222
cheader = str_columns[i]
223223

224-
max_colwidth = max(_strlen(x) for x in cheader)
224+
max_colwidth = max(self.col_space or 0, *(_strlen(x) for x in cheader))
225225

226226
fmt_values = _make_fixed_width(fmt_values, self.justify,
227227
minimum=max_colwidth)
@@ -431,6 +431,11 @@ def write(self, s, indent=0):
431431
self.elements.append(' ' * indent + com.pprint_thing(s))
432432

433433
def write_th(self, s, indent=0, tags=None):
434+
if (self.fmt.col_space is not None
435+
and self.fmt.col_space > 0 ):
436+
tags = (tags or "" )
437+
tags += 'style="min-width: %s;"' % self.fmt.col_space
438+
434439
return self._write_cell(s, kind='th', indent=indent, tags=tags)
435440

436441
def write_td(self, s, indent=0, tags=None):

pandas/tests/test_format.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ def test_to_string_buffer_all_unicode(self):
183183
# this should work
184184
buf.getvalue()
185185

186+
def test_to_string_with_col_space(self):
187+
df = DataFrame(np.random.random(size=(1,3)))
188+
c10=len(df.to_string(col_space=10).split("\n")[1])
189+
c20=len(df.to_string(col_space=20).split("\n")[1])
190+
c30=len(df.to_string(col_space=30).split("\n")[1])
191+
self.assertTrue( c10 < c20 < c30 )
192+
193+
def test_to_html_with_col_space(self):
194+
def check_with_width(df,col_space):
195+
import re
196+
# check that col_space affects HTML generation
197+
# and be very brittle about it.
198+
html = df.to_html(col_space=col_space)
199+
hdrs = [x for x in html.split("\n") if re.search("<th[>\s]",x)]
200+
self.assertTrue(len(hdrs) > 0 )
201+
for h in hdrs:
202+
self.assertTrue("min-width" in h )
203+
self.assertTrue(str(col_space) in h )
204+
205+
df = DataFrame(np.random.random(size=(1,3)))
206+
207+
check_with_width(df,30)
208+
check_with_width(df,50)
209+
186210
def test_to_html_unicode(self):
187211
# it works!
188212
df = DataFrame({u'\u03c3' : np.arange(10.)})

0 commit comments

Comments
 (0)