@@ -41,6 +41,12 @@ def __init__(self, formatter, classes=None, notebook=False, border=None,
41
41
border = get_option ('display.html.border' )
42
42
self .border = border
43
43
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.
44
50
self .show_col_idx_names = all ((self .fmt .has_column_names ,
45
51
self .fmt .show_index_names ,
46
52
self .fmt .header ))
@@ -201,6 +207,17 @@ def write_result(self, buf):
201
207
202
208
def _write_header (self , indent ):
203
209
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
204
221
if self .fmt .index :
205
222
row_levels = self .frame .index .nlevels
206
223
else :
@@ -271,8 +288,21 @@ def _write_header(self, indent):
271
288
values = (values [:ins_col ] + [u ('...' )] +
272
289
values [ins_col :])
273
290
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.
274
297
row = ['' ] * (row_levels - 1 )
275
298
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.
276
306
if self .fmt .show_index_names :
277
307
name = self .columns .names [lnum ]
278
308
row .append (pprint_thing (name or '' ))
@@ -292,8 +322,19 @@ def _write_header(self, indent):
292
322
self .write_tr (row , indent , self .indent_delta , tags = tags ,
293
323
header = True )
294
324
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.
295
331
row = ['' ] * (row_levels - 1 )
296
332
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.
297
338
if self .fmt .show_index_names :
298
339
row .append (self .columns .name or '' )
299
340
else :
@@ -352,6 +393,13 @@ def _write_regular_rows(self, fmt_values, indent):
352
393
index_values = self .fmt .tr_frame .index .format ()
353
394
row_levels = 1
354
395
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
355
403
row_levels = 1 if self .show_col_idx_names else 0
356
404
357
405
row = []
@@ -365,6 +413,10 @@ def _write_regular_rows(self, fmt_values, indent):
365
413
row = []
366
414
if self .fmt .index :
367
415
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.
368
420
elif self .show_col_idx_names :
369
421
row .append ('' )
370
422
row .extend (fmt_values [j ][i ] for j in range (self .ncols ))
0 commit comments