Skip to content

Commit 85c1f3c

Browse files
committed
BUG: Prevent index header column from being added on MultiIndex when index=False. pandas-dev#8452
1 parent 2e63fe2 commit 85c1f3c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Bug Fixes
155155
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)
156156
- Fix: The font size was only set on x axis if vertical or the y axis if horizontal. (:issue:`8765`)
157157
- Fixed division by 0 when reading big csv files in python 3 (:issue:`8621`)
158+
- Fixed Multindex to_html index=False adds an extra column (:issue:`8452`)
158159

159160

160161

pandas/core/format.py

+4
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,10 @@ def _column_header():
959959
name = self.columns.names[lnum]
960960
row = [''] * (row_levels - 1) + ['' if name is None
961961
else com.pprint_thing(name)]
962+
963+
if row == [""] and self.fmt.index is False:
964+
row = []
965+
962966
tags = {}
963967
j = len(row)
964968
for i, v in enumerate(values):

pandas/tests/test_format.py

+41
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,47 @@ def test_to_html_escape_disabled(self):
571571
</table>"""
572572
self.assertEqual(xp, rs)
573573

574+
def test_to_html_multiindex_index_false(self):
575+
# issue 8452
576+
df = pd.DataFrame({
577+
'a': range(2),
578+
'b': range(3, 5),
579+
'c': range(5, 7),
580+
'd': range(3, 5)}
581+
)
582+
df.columns = pd.MultiIndex.from_product([['a', 'b'], ['c', 'd']])
583+
result = df.to_html(index=False)
584+
expected = """\
585+
<table border="1" class="dataframe">
586+
<thead>
587+
<tr>
588+
<th colspan="2" halign="left">a</th>
589+
<th colspan="2" halign="left">b</th>
590+
</tr>
591+
<tr>
592+
<th>c</th>
593+
<th>d</th>
594+
<th>c</th>
595+
<th>d</th>
596+
</tr>
597+
</thead>
598+
<tbody>
599+
<tr>
600+
<td> 0</td>
601+
<td> 3</td>
602+
<td> 5</td>
603+
<td> 3</td>
604+
</tr>
605+
<tr>
606+
<td> 1</td>
607+
<td> 4</td>
608+
<td> 6</td>
609+
<td> 4</td>
610+
</tr>
611+
</tbody>
612+
</table>"""
613+
self.assertEqual(result, expected)
614+
574615
def test_to_html_multiindex_sparsify_false_multi_sparse(self):
575616
with option_context('display.multi_sparse', False):
576617
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],

0 commit comments

Comments
 (0)