Skip to content

BUG: df.to_html(index=False) renders index.name #10346

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
Aug 5, 2015
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ Performance Improvements
Bug Fixes
~~~~~~~~~


- Bug in ``DataFrame.to_html(index=False)`` renders unnecessary ``name`` row (:issue:`10344`)
- Bug in ``DataFrame.apply`` when function returns categorical series. (:issue:`9573`)
- Bug in ``to_datetime`` with invalid dates and formats supplied (:issue:`10154`)
- Bug in ``Index.drop_duplicates`` dropping name(s) (:issue:`10115`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def _column_header():
self.write_tr(col_row, indent, self.indent_delta, header=True,
align=align)

if self.fmt.has_index_names:
if self.fmt.has_index_names and self.fmt.index:
row = [
x if x is not None else '' for x in self.frame.index.names
] + [''] * min(len(self.columns), self.max_cols)
Expand Down
184 changes: 184 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ def test_to_html_multiindex_index_false(self):
</table>"""
self.assertEqual(result, expected)

df.index = Index(df.index.values, name='idx')
result = df.to_html(index=False)
self.assertEqual(result, expected)

def test_to_html_multiindex_sparsify_false_multi_sparse(self):
with option_context('display.multi_sparse', False):
index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
Expand Down Expand Up @@ -1922,15 +1926,195 @@ def test_to_html_index(self):
'C': ['one', 'two', np.NaN]},
columns=['A', 'B', 'C'],
index=index)
expected_with_index = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <th>foo</th>\n'
' <td>1</td>\n'
' <td>1.2</td>\n'
' <td>one</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bar</th>\n'
' <td>2</td>\n'
' <td>3.4</td>\n'
' <td>two</td>\n'
' </tr>\n'
' <tr>\n'
' <th>baz</th>\n'
' <td>3</td>\n'
' <td>5.6</td>\n'
' <td>NaN</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
self.assertEqual(df.to_html(), expected_with_index)

expected_without_index = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <td>1</td>\n'
' <td>1.2</td>\n'
' <td>one</td>\n'
' </tr>\n'
' <tr>\n'
' <td>2</td>\n'
' <td>3.4</td>\n'
' <td>two</td>\n'
' </tr>\n'
' <tr>\n'
' <td>3</td>\n'
' <td>5.6</td>\n'
' <td>NaN</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
result = df.to_html(index=False)
for i in index:
self.assertNotIn(i, result)
self.assertEqual(result, expected_without_index)
df.index = Index(['foo', 'bar', 'baz'], name='idx')
expected_with_index = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' <tr>\n'
' <th>idx</th>\n'
' <th></th>\n'
' <th></th>\n'
' <th></th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <th>foo</th>\n'
' <td>1</td>\n'
' <td>1.2</td>\n'
' <td>one</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bar</th>\n'
' <td>2</td>\n'
' <td>3.4</td>\n'
' <td>two</td>\n'
' </tr>\n'
' <tr>\n'
' <th>baz</th>\n'
' <td>3</td>\n'
' <td>5.6</td>\n'
' <td>NaN</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
self.assertEqual(df.to_html(), expected_with_index)
self.assertEqual(df.to_html(index=False), expected_without_index)

tuples = [('foo', 'car'), ('foo', 'bike'), ('bar', 'car')]
df.index = MultiIndex.from_tuples(tuples)

expected_with_index = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th></th>\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <th rowspan="2" valign="top">foo</th>\n'
' <th>car</th>\n'
' <td>1</td>\n'
' <td>1.2</td>\n'
' <td>one</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bike</th>\n'
' <td>2</td>\n'
' <td>3.4</td>\n'
' <td>two</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bar</th>\n'
' <th>car</th>\n'
' <td>3</td>\n'
' <td>5.6</td>\n'
' <td>NaN</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
self.assertEqual(df.to_html(), expected_with_index)

result = df.to_html(index=False)
for i in ['foo', 'bar', 'car', 'bike']:
self.assertNotIn(i, result)
# must be the same result as normal index
self.assertEqual(result, expected_without_index)

df.index = MultiIndex.from_tuples(tuples, names=['idx1', 'idx2'])
expected_with_index = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th></th>\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' <tr>\n'
' <th>idx1</th>\n'
' <th>idx2</th>\n'
' <th></th>\n'
' <th></th>\n'
' <th></th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <th rowspan="2" valign="top">foo</th>\n'
' <th>car</th>\n'
' <td>1</td>\n'
' <td>1.2</td>\n'
' <td>one</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bike</th>\n'
' <td>2</td>\n'
' <td>3.4</td>\n'
' <td>two</td>\n'
' </tr>\n'
' <tr>\n'
' <th>bar</th>\n'
' <th>car</th>\n'
' <td>3</td>\n'
' <td>5.6</td>\n'
' <td>NaN</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
self.assertEqual(df.to_html(), expected_with_index)
self.assertEqual(df.to_html(index=False), expected_without_index)

def test_repr_html(self):
self.frame._repr_html_()
Expand Down