Skip to content

Commit d3986f0

Browse files
add code comments
1 parent d60fa0b commit d3986f0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

pandas/io/formats/html.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def __init__(self, formatter, classes=None, notebook=False, border=None,
4141
border = get_option('display.html.border')
4242
self.border = border
4343
self.table_id = table_id
44+
# see gh-22579
45+
# Column misalignment also occurs for
46+
# a standard index when the columns index is named.
47+
# Determine if ANY column names need to be displayed
48+
# since if the row index is not displayed a column of
49+
# blank cells need to be included before the DataFrame values.
4450
self.show_col_idx_names = all((self.fmt.has_column_names,
4551
self.fmt.show_index_names,
4652
self.fmt.header))
@@ -201,6 +207,17 @@ def write_result(self, buf):
201207

202208
def _write_header(self, indent):
203209
truncate_h = self.fmt.truncate_h
210+
# see gh-22579
211+
# Column Offset Bug with to_html(index=False) with
212+
# MultiIndex Columns and Index.
213+
# Column misalignment also occurs for
214+
# a standard index when the columns index is named.
215+
# If the row index is not displayed a column of
216+
# blank cells need to be included before the DataFrame values.
217+
# However, in this code block only the placement of the truncation
218+
# indicators within the header is affected by this.
219+
# TODO: refactor to class property as row_levels also used in
220+
# _write_regular_rows and _write_hierarchical_rows
204221
if self.fmt.index:
205222
row_levels = self.frame.index.nlevels
206223
else:
@@ -271,8 +288,21 @@ def _write_header(self, indent):
271288
values = (values[:ins_col] + [u('...')] +
272289
values[ins_col:])
273290

291+
# see gh-22579
292+
# Column Offset Bug with to_html(index=False) with
293+
# MultiIndex Columns and Index.
294+
# Initially fill row with blank cells before column names.
295+
# TODO: Refactor to remove code duplication with code
296+
# block below for standard columns index.
274297
row = [''] * (row_levels - 1)
275298
if self.fmt.index or self.show_col_idx_names:
299+
# see gh-22747
300+
# If to_html(index_names=False) do not show columns
301+
# index names.
302+
# TODO: Refactor to use _get_column_name_list from
303+
# DataFrameFormatter class and create a
304+
# _get_formatted_column_labels function for code
305+
# parity with DataFrameFormatter class.
276306
if self.fmt.show_index_names:
277307
name = self.columns.names[lnum]
278308
row.append(pprint_thing(name or ''))
@@ -292,8 +322,19 @@ def _write_header(self, indent):
292322
self.write_tr(row, indent, self.indent_delta, tags=tags,
293323
header=True)
294324
else:
325+
# see gh-22579
326+
# Column misalignment also occurs for
327+
# a standard index when the columns index is named.
328+
# Initially fill row with blank cells before column names.
329+
# TODO: Refactor to remove code duplication with code block
330+
# above for columns MultiIndex.
295331
row = [''] * (row_levels - 1)
296332
if self.fmt.index or self.show_col_idx_names:
333+
# see gh-22747
334+
# If to_html(index_names=False) do not show columns
335+
# index names.
336+
# TODO: Refactor to use _get_column_name_list from
337+
# DataFrameFormatter class.
297338
if self.fmt.show_index_names:
298339
row.append(self.columns.name or '')
299340
else:
@@ -352,6 +393,13 @@ def _write_regular_rows(self, fmt_values, indent):
352393
index_values = self.fmt.tr_frame.index.format()
353394
row_levels = 1
354395
else:
396+
# see gh-22579
397+
# Column misalignment also occurs for
398+
# a standard index when the columns index is named.
399+
# row_levels is used for the number of <th> cells and
400+
# the placement of the truncation indicators.
401+
# TODO: refactor to class property as row_levels also used in
402+
# _write_header and _write_hierarchical_rows
355403
row_levels = 1 if self.show_col_idx_names else 0
356404

357405
row = []
@@ -365,6 +413,10 @@ def _write_regular_rows(self, fmt_values, indent):
365413
row = []
366414
if self.fmt.index:
367415
row.append(index_values[i])
416+
# see gh-22579
417+
# Column misalignment also occurs for
418+
# a standard index when the columns index is named.
419+
# Add blank cell before data cells.
368420
elif self.show_col_idx_names:
369421
row.append('')
370422
row.extend(fmt_values[j][i] for j in range(self.ncols))

0 commit comments

Comments
 (0)