From a24ca836d99a0a6ca481c3e6c5f822f335683553 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 2 Jan 2019 11:55:22 +0000 Subject: [PATCH 1/2] BUG: (row) Index Name with to_html(header=False) is not displayed --- pandas/io/formats/html.py | 46 ++++++++------ .../html/index_named_multi_columns_none.html | 23 +++++++ .../index_named_standard_columns_none.html | 21 +++++++ .../index_unnamed_multi_columns_none.html | 15 +++++ .../index_unnamed_standard_columns_none.html | 14 +++++ ...unc_df_index_named_multi_columns_none.html | 62 +++++++++++++++++++ ..._df_index_named_standard_columns_none.html | 54 ++++++++++++++++ pandas/tests/io/formats/test_to_html.py | 22 ++++--- 8 files changed, 231 insertions(+), 26 deletions(-) create mode 100644 pandas/tests/io/formats/data/html/index_named_multi_columns_none.html create mode 100644 pandas/tests/io/formats/data/html/index_named_standard_columns_none.html create mode 100644 pandas/tests/io/formats/data/html/index_unnamed_multi_columns_none.html create mode 100644 pandas/tests/io/formats/data/html/index_unnamed_standard_columns_none.html create mode 100644 pandas/tests/io/formats/data/html/trunc_df_index_named_multi_columns_none.html create mode 100644 pandas/tests/io/formats/data/html/trunc_df_index_named_standard_columns_none.html diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index eb11dd461927b..58f5364f2b523 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -43,6 +43,12 @@ def __init__(self, formatter, classes=None, notebook=False, border=None, self.table_id = table_id self.render_links = render_links + @property + def show_row_idx_names(self): + return all((self.fmt.has_index_names, + self.fmt.index, + self.fmt.show_index_names)) + @property def show_col_idx_names(self): # see gh-22579 @@ -165,9 +171,7 @@ def write_style(self): element_props.append(('thead tr th', 'text-align', 'left')) - if all((self.fmt.has_index_names, - self.fmt.index, - self.fmt.show_index_names)): + if self.show_row_idx_names: element_props.append(('thead tr:last-of-type th', 'text-align', 'right')) @@ -228,17 +232,8 @@ def write_result(self, buf): buffer_put_lines(buf, self.elements) - def _write_header(self, indent): + def _write_col_header(self, indent): truncate_h = self.fmt.truncate_h - - if not self.fmt.header: - # write nothing - return indent - - self.write('', indent) - - indent += self.indent_delta - if isinstance(self.columns, ABCMultiIndex): template = 'colspan="{span:d}" halign="left"' @@ -357,12 +352,25 @@ def _write_header(self, indent): self.write_tr(row, indent, self.indent_delta, header=True, align=align) - if all((self.fmt.has_index_names, - self.fmt.index, - self.fmt.show_index_names)): - row = ([x if x is not None else '' for x in self.frame.index.names] - + [''] * (self.ncols + (1 if truncate_h else 0))) - self.write_tr(row, indent, self.indent_delta, header=True) + def _write_row_header(self, indent): + truncate_h = self.fmt.truncate_h + row = ([x if x is not None else '' for x in self.frame.index.names] + + [''] * (self.ncols + (1 if truncate_h else 0))) + self.write_tr(row, indent, self.indent_delta, header=True) + + def _write_header(self, indent): + if not (self.fmt.header or self.show_row_idx_names): + # write nothing + return indent + + self.write('', indent) + indent += self.indent_delta + + if self.fmt.header: + self._write_col_header(indent) + + if self.show_row_idx_names: + self._write_row_header(indent) indent -= self.indent_delta self.write('', indent) diff --git a/pandas/tests/io/formats/data/html/index_named_multi_columns_none.html b/pandas/tests/io/formats/data/html/index_named_multi_columns_none.html new file mode 100644 index 0000000000000..8c41d2e29f2c0 --- /dev/null +++ b/pandas/tests/io/formats/data/html/index_named_multi_columns_none.html @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + +
index.name.0index.name.1
ab00
c00
diff --git a/pandas/tests/io/formats/data/html/index_named_standard_columns_none.html b/pandas/tests/io/formats/data/html/index_named_standard_columns_none.html new file mode 100644 index 0000000000000..432d8e06d5784 --- /dev/null +++ b/pandas/tests/io/formats/data/html/index_named_standard_columns_none.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +
index.name
000
100
diff --git a/pandas/tests/io/formats/data/html/index_unnamed_multi_columns_none.html b/pandas/tests/io/formats/data/html/index_unnamed_multi_columns_none.html new file mode 100644 index 0000000000000..81da7c3619abc --- /dev/null +++ b/pandas/tests/io/formats/data/html/index_unnamed_multi_columns_none.html @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + +
ab00
c00
diff --git a/pandas/tests/io/formats/data/html/index_unnamed_standard_columns_none.html b/pandas/tests/io/formats/data/html/index_unnamed_standard_columns_none.html new file mode 100644 index 0000000000000..3d958afe4a4ac --- /dev/null +++ b/pandas/tests/io/formats/data/html/index_unnamed_standard_columns_none.html @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +
000
100
diff --git a/pandas/tests/io/formats/data/html/trunc_df_index_named_multi_columns_none.html b/pandas/tests/io/formats/data/html/trunc_df_index_named_multi_columns_none.html new file mode 100644 index 0000000000000..0f262495b6c6b --- /dev/null +++ b/pandas/tests/io/formats/data/html/trunc_df_index_named_multi_columns_none.html @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
foobaz
ace01...67
f89...1415
........................
bde4849...5455
f5657...6263
diff --git a/pandas/tests/io/formats/data/html/trunc_df_index_named_standard_columns_none.html b/pandas/tests/io/formats/data/html/trunc_df_index_named_standard_columns_none.html new file mode 100644 index 0000000000000..d294a507dbce4 --- /dev/null +++ b/pandas/tests/io/formats/data/html/trunc_df_index_named_standard_columns_none.html @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
index.name
001...67
189...1415
..................
64849...5455
75657...6263
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 213eb0d5b5cb8..d333330c19e39 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -429,6 +429,7 @@ def test_to_html_multi_indexes_index_false(self, datapath): assert result == expected @pytest.mark.parametrize('index_names', [True, False]) + @pytest.mark.parametrize('header', [True, False]) @pytest.mark.parametrize('index', [True, False]) @pytest.mark.parametrize('column_index, column_type', [ (Index([0, 1]), 'unnamed_standard'), @@ -448,18 +449,21 @@ def test_to_html_multi_indexes_index_false(self, datapath): ]) def test_to_html_basic_alignment( self, datapath, row_index, row_type, column_index, column_type, - index, index_names): + index, header, index_names): # GH 22747, GH 22579 df = DataFrame(np.zeros((2, 2), dtype=int), index=row_index, columns=column_index) - result = df.to_html(index=index, index_names=index_names) + result = df.to_html( + index=index, header=header, index_names=index_names) if not index: row_type = 'none' elif not index_names and row_type.startswith('named'): row_type = 'un' + row_type - if not index_names and column_type.startswith('named'): + if not header: + column_type = 'none' + elif not index_names and column_type.startswith('named'): column_type = 'un' + column_type filename = 'index_' + row_type + '_columns_' + column_type @@ -467,6 +471,7 @@ def test_to_html_basic_alignment( assert result == expected @pytest.mark.parametrize('index_names', [True, False]) + @pytest.mark.parametrize('header', [True, False]) @pytest.mark.parametrize('index', [True, False]) @pytest.mark.parametrize('column_index, column_type', [ (Index(np.arange(8)), 'unnamed_standard'), @@ -488,19 +493,22 @@ def test_to_html_basic_alignment( ]) def test_to_html_alignment_with_truncation( self, datapath, row_index, row_type, column_index, column_type, - index, index_names): + index, header, index_names): # GH 22747, GH 22579 df = DataFrame(np.arange(64).reshape(8, 8), index=row_index, columns=column_index) - result = df.to_html(max_rows=4, max_cols=4, - index=index, index_names=index_names) + result = df.to_html( + max_rows=4, max_cols=4, + index=index, header=header, index_names=index_names) if not index: row_type = 'none' elif not index_names and row_type.startswith('named'): row_type = 'un' + row_type - if not index_names and column_type.startswith('named'): + if not header: + column_type = 'none' + elif not index_names and column_type.startswith('named'): column_type = 'un' + column_type filename = 'trunc_df_index_' + row_type + '_columns_' + column_type From c9ccd883512f7d7cc28505ae7f36c41d8555e99e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 2 Jan 2019 13:02:35 +0000 Subject: [PATCH 2/2] add whatsnew --- doc/source/whatsnew/v0.24.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 302f2bd05ee5c..43229f3f674d0 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1600,6 +1600,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form - Bug in :func:`to_html()` with ``index=False`` misses truncation indicators (...) on truncated DataFrame (:issue:`15019`, :issue:`22783`) - Bug in :func:`to_html()` with ``index=False`` when both columns and row index are ``MultiIndex`` (:issue:`22579`) - Bug in :func:`to_html()` with ``index_names=False`` displaying index name (:issue:`22747`) +- Bug in :func:`to_html()` with ``header=False`` not displaying row index names (:issue:`23788`) - Bug in :func:`DataFrame.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`) - Bug in :func:`DataFrame.to_string()` that caused representations of :class:`DataFrame` to not take up the whole window (:issue:`22984`) - Bug in :func:`DataFrame.to_csv` where a single level MultiIndex incorrectly wrote a tuple. Now just the value of the index is written (:issue:`19589`).