From 8677ed847624a3dbd58f64475fded841d15abfbd Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 22 Nov 2012 18:33:51 +0200 Subject: [PATCH 1/5] TST: df.to_string() should respect self.col_space --- pandas/tests/test_format.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 10bb75bfbb5b6..41e6d7a50c511 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -182,6 +182,13 @@ 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_unicode(self): # it works! df = DataFrame({u'\u03c3' : np.arange(10.)}) From 03a31cc84be5c184ae51ac2777bf3b9ae3386d04 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 22 Nov 2012 20:13:07 +0200 Subject: [PATCH 2/5] BUG: df.to_string() should respect self.col_space, #1000 --- pandas/core/format.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/format.py b/pandas/core/format.py index 13e504a8e1f88..cbdc49af46dbc 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -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 @@ -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) From f187a3a912b6ba2b9e3b3ce4e292e1aeca3a4624 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 22 Nov 2012 19:05:27 +0200 Subject: [PATCH 3/5] TST: df.to_html() should respect col_space argument --- pandas/tests/test_format.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 41e6d7a50c511..8d0dacf2e7edd 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -189,6 +189,23 @@ def test_to_string_with_col_space(self): 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("\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.)}) From 875a0e9865f124bda4862b2bd4c3e98b4c577a13 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 22 Nov 2012 20:19:56 +0200 Subject: [PATCH 4/5] BUG: df.to_html() should respect col_space argument. #1000 --- pandas/core/format.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/core/format.py b/pandas/core/format.py index cbdc49af46dbc..841500329d4a9 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -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): From efe48a595ea7f5bb3d4f2ee116b8f423fad321d8 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 22 Nov 2012 19:23:39 +0200 Subject: [PATCH 5/5] DOC: change definition of col_space. wasn't implemented till now, so shouldn't break anything. colSpace was already deprecated in favor of col_space, so I would feel bad about deprecating col_space in favor of min_col_width. --- doc/source/io.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index f74120ad7ef57..3fbc45dda8fa4 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -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