diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 9dc2692f276e3..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('') @@ -793,22 +793,33 @@ 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(). 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] @@ -816,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: diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 390c3f3d5c709..90f1dbe704806 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): @@ -184,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('
{rows} rows {by} {cols} columns
') + .format(rows=len(self.frame), + by=by, + cols=len(self.frame.columns))) + if self.notebook: + self.write('{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('