Skip to content
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

Fixes for #1000, to_string(), to_html() should respect col_space #2328

Merged
merged 5 commits into from Nov 29, 2012
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
2 changes: 1 addition & 1 deletion doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ over the string representation of the object. All arguments are optional:

- ``buf`` default None, for example a StringIO object
- ``columns`` default None, which columns to write
- ``col_space`` default None, number of spaces to write between columns
- ``col_space`` default None, minimum width of each column.
- ``na_rep`` default ``NaN``, representation of NA value
- ``formatters`` default None, a dictionary (by column) of functions each of
which takes a single argument and returns a formatted string
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
columns : sequence, optional
the subset of columns to write; default None writes all columns
col_space : int, optional
the width of each columns
the minimum width of each column
header : bool, optional
whether to print column labels, default True
index : bool, optional
Expand Down Expand Up @@ -215,7 +215,7 @@ def _to_str_columns(self, force_unicode=False):
fmt_values = self._format_col(i)
cheader = str_columns[i]

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

fmt_values = _make_fixed_width(fmt_values, self.justify,
minimum=max_colwidth)
Expand Down Expand Up @@ -434,6 +434,11 @@ def write(self, s, indent=0):
self.elements.append(' ' * indent + com.pprint_thing(s))

def write_th(self, s, indent=0, tags=None):
if (self.fmt.col_space is not None
and self.fmt.col_space > 0 ):
tags = (tags or "" )
tags += 'style="min-width: %s;"' % self.fmt.col_space

return self._write_cell(s, kind='th', indent=indent, tags=tags)

def write_td(self, s, indent=0, tags=None):
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ def test_to_string_buffer_all_unicode(self):
# this should work
buf.getvalue()

def test_to_string_with_col_space(self):
df = DataFrame(np.random.random(size=(1,3)))
c10=len(df.to_string(col_space=10).split("\n")[1])
c20=len(df.to_string(col_space=20).split("\n")[1])
c30=len(df.to_string(col_space=30).split("\n")[1])
self.assertTrue( c10 < c20 < c30 )

def test_to_html_with_col_space(self):
def check_with_width(df,col_space):
import re
# check that col_space affects HTML generation
# and be very brittle about it.
html = df.to_html(col_space=col_space)
hdrs = [x for x in html.split("\n") if re.search("<th[>\s]",x)]
self.assertTrue(len(hdrs) > 0 )
for h in hdrs:
self.assertTrue("min-width" in h )
self.assertTrue(str(col_space) in h )

df = DataFrame(np.random.random(size=(1,3)))

check_with_width(df,30)
check_with_width(df,50)

def test_to_html_unicode(self):
# it works!
df = DataFrame({u'\u03c3' : np.arange(10.)})
Expand Down