Skip to content

BUG: Prevent index header column from being added on MultiIndex when ind... #9018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Bug Fixes
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)
- Fix: The font size was only set on x axis if vertical or the y axis if horizontal. (:issue:`8765`)
- Fixed division by 0 when reading big csv files in python 3 (:issue:`8621`)
- Fixed Multindex to_html index=False adds an extra column (:issue:`8452`)



Expand Down
4 changes: 4 additions & 0 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,10 @@ def _column_header():
name = self.columns.names[lnum]
row = [''] * (row_levels - 1) + ['' if name is None
else com.pprint_thing(name)]

if row == [""] and self.fmt.index is False:
row = []

tags = {}
j = len(row)
for i, v in enumerate(values):
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,47 @@ def test_to_html_escape_disabled(self):
</table>"""
self.assertEqual(xp, rs)

def test_to_html_multiindex_index_false(self):
# issue 8452
df = pd.DataFrame({
'a': range(2),
'b': range(3, 5),
'c': range(5, 7),
'd': range(3, 5)}
)
df.columns = pd.MultiIndex.from_product([['a', 'b'], ['c', 'd']])
result = df.to_html(index=False)
expected = """\
<table border="1" class="dataframe">
<thead>
<tr>
<th colspan="2" halign="left">a</th>
<th colspan="2" halign="left">b</th>
</tr>
<tr>
<th>c</th>
<th>d</th>
<th>c</th>
<th>d</th>
</tr>
</thead>
<tbody>
<tr>
<td> 0</td>
<td> 3</td>
<td> 5</td>
<td> 3</td>
</tr>
<tr>
<td> 1</td>
<td> 4</td>
<td> 6</td>
<td> 4</td>
</tr>
</tbody>
</table>"""
self.assertEqual(result, expected)

def test_to_html_multiindex_sparsify_false_multi_sparse(self):
with option_context('display.multi_sparse', False):
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
Expand Down