From 6b7f9d11a1b303c932fb3d0c4552cd9d2dbaefe1 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 5 Jan 2019 10:12:21 +0000 Subject: [PATCH 1/4] move properties to DataFrameFormatter --- pandas/io/formats/format.py | 12 ++++++++++++ pandas/io/formats/html.py | 16 ++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 9dc2692f276e3..a5620b3cb4a44 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -793,6 +793,18 @@ def has_index_names(self): def has_column_names(self): return _has_names(self.frame.columns) + @property + def show_row_idx_names(self): + return all((self.has_index_names, + self.index, + self.show_index_names)) + + @property + def show_col_idx_names(self): + return all((self.has_column_names, + self.show_index_names, + self.header)) + def _get_formatted_index(self, frame): # Note: this is only used by to_string() and to_latex(), not by # to_html(). diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 390c3f3d5c709..edf1239a9c57b 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -45,23 +45,11 @@ def __init__(self, formatter, classes=None, notebook=False, border=None, @property def show_row_idx_names(self): - return all((self.fmt.has_index_names, - self.fmt.index, - self.fmt.show_index_names)) + return self.fmt.show_row_idx_names @property def show_col_idx_names(self): - # see gh-22579 - # Column misalignment also occurs for - # a standard index when the columns index is named. - # Determine if ANY column names need to be displayed - # since if the row index is not displayed a column of - # blank cells need to be included before the DataFrame values. - # TODO: refactor to add show_col_idx_names property to - # DataFrameFormatter - return all((self.fmt.has_column_names, - self.fmt.show_index_names, - self.fmt.header)) + return self.fmt.show_col_idx_names @property def row_levels(self): From a97ce1ca7af65035a6f8bed6516929eca0d08b33 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 5 Jan 2019 10:39:44 +0000 Subject: [PATCH 2/4] use class property in _get_formatted_index --- pandas/io/formats/format.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index a5620b3cb4a44..c75de7e283f6b 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -810,17 +810,16 @@ def _get_formatted_index(self, frame): # to_html(). index = frame.index columns = frame.columns - - show_index_names = self.show_index_names and self.has_index_names - show_col_names = (self.show_index_names and self.has_column_names) - fmt = self._get_formatter('__index__') if isinstance(index, ABCMultiIndex): - fmt_index = index.format(sparsify=self.sparsify, adjoin=False, - names=show_index_names, formatter=fmt) + fmt_index = index.format( + sparsify=self.sparsify, adjoin=False, + names=self.show_row_idx_names, formatter=fmt) else: - fmt_index = [index.format(name=show_index_names, formatter=fmt)] + fmt_index = [index.format( + name=self.show_row_idx_names, formatter=fmt)] + fmt_index = [tuple(_make_fixed_width(list(x), justify='left', minimum=(self.col_space or 0), adj=self.adj)) for x in fmt_index] @@ -828,7 +827,7 @@ def _get_formatted_index(self, frame): adjoined = self.adj.adjoin(1, *fmt_index).split('\n') # empty space for columns - if show_col_names: + if self.show_col_idx_names: col_header = ['{x}'.format(x=x) for x in self._get_column_name_list()] else: From e713149ebac28c5001dccde019d64acec9f8d7de Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 5 Jan 2019 11:18:32 +0000 Subject: [PATCH 3/4] use class property in _get_formatted_column_labels --- pandas/io/formats/format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index c75de7e283f6b..f8ee9c273fd59 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -778,7 +778,7 @@ def space_format(x, y): for i, (col, x) in enumerate(zip(columns, fmt_columns))] - if self.show_index_names and self.has_index_names: + if self.show_row_idx_names: for x in str_columns: x.append('') From 5825c18e8f4b7a106e5206ff3ee88459dd584e44 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 5 Jan 2019 12:33:57 +0000 Subject: [PATCH 4/4] split write_result --- pandas/io/formats/html.py | 76 +++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index edf1239a9c57b..90f1dbe704806 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -172,14 +172,28 @@ def write_style(self): template = dedent('\n'.join((template_first, template_mid, template_last))) - if self.notebook: - self.write(template) + self.write(template) def write_result(self, buf): - indent = 0 - id_section = "" - frame = self.frame + if self.notebook: + self.write('
') + self.write_style() + + self._write_table() + + if self.should_show_dimensions: + by = chr(215) if compat.PY3 else unichr(215) # × + self.write(u('

{rows} rows {by} {cols} columns

') + .format(rows=len(self.frame), + by=by, + cols=len(self.frame.columns))) + if self.notebook: + self.write('
') + + buffer_put_lines(buf, self.elements) + + def _write_table(self, indent=0): _classes = ['dataframe'] # Default class. use_mathjax = get_option("display.html.use_mathjax") if not use_mathjax: @@ -192,33 +206,21 @@ def write_result(self, buf): .format(typ=type(self.classes))) _classes.extend(self.classes) - if self.notebook: - self.write('
') - - self.write_style() - - if self.table_id is not None: + if self.table_id is None: + id_section = "" + else: id_section = ' id="{table_id}"'.format(table_id=self.table_id) + self.write('' .format(border=self.border, cls=' '.join(_classes), id_section=id_section), indent) - indent += self.indent_delta - indent = self._write_header(indent) - indent = self._write_body(indent) + if self.fmt.header or self.show_row_idx_names: + self._write_header(indent + self.indent_delta) - self.write('
', indent) - if self.should_show_dimensions: - by = chr(215) if compat.PY3 else unichr(215) # × - self.write(u('

{rows} rows {by} {cols} columns

') - .format(rows=len(frame), - by=by, - cols=len(frame.columns))) + self._write_body(indent + self.indent_delta) - if self.notebook: - self.write('
') - - buffer_put_lines(buf, self.elements) + self.write('', indent) def _write_col_header(self, indent): truncate_h = self.fmt.truncate_h @@ -347,41 +349,29 @@ def _write_row_header(self, indent): 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) + self._write_col_header(indent + self.indent_delta) if self.show_row_idx_names: - self._write_row_header(indent) + self._write_row_header(indent + self.indent_delta) - indent -= self.indent_delta self.write('', indent) - return indent - def _write_body(self, indent): self.write('', indent) - indent += self.indent_delta - fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)} # write values if self.fmt.index and isinstance(self.frame.index, ABCMultiIndex): - self._write_hierarchical_rows(fmt_values, indent) + self._write_hierarchical_rows( + fmt_values, indent + self.indent_delta) else: - self._write_regular_rows(fmt_values, indent) + self._write_regular_rows( + fmt_values, indent + self.indent_delta) - indent -= self.indent_delta self.write('', indent) - indent -= self.indent_delta - - return indent def _write_regular_rows(self, fmt_values, indent): truncate_h = self.fmt.truncate_h