Skip to content

Commit 2a2a6d1

Browse files
committed
BUG: df.to_html(index=False) renders index.name
1 parent b1a1613 commit 2a2a6d1

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ Performance Improvements
464464
Bug Fixes
465465
~~~~~~~~~
466466

467+
468+
- Bug in ``DataFrame.to_html(index=False)`` renders unnecessary ``name`` row (:issue:`10344`)
467469
- Bug in ``DataFrame.apply`` when function returns categorical series. (:issue:`9573`)
468470
- Bug in ``to_datetime`` with invalid dates and formats supplied (:issue:`10154`)
469471
- Bug in ``Index.drop_duplicates`` dropping name(s) (:issue:`10115`)

pandas/core/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def _column_header():
10371037
self.write_tr(col_row, indent, self.indent_delta, header=True,
10381038
align=align)
10391039

1040-
if self.fmt.has_index_names:
1040+
if self.fmt.has_index_names and self.fmt.index:
10411041
row = [
10421042
x if x is not None else '' for x in self.frame.index.names
10431043
] + [''] * min(len(self.columns), self.max_cols)

pandas/tests/test_format.py

+184
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ def test_to_html_multiindex_index_false(self):
632632
</table>"""
633633
self.assertEqual(result, expected)
634634

635+
df.index = Index(df.index.values, name='idx')
636+
result = df.to_html(index=False)
637+
self.assertEqual(result, expected)
638+
635639
def test_to_html_multiindex_sparsify_false_multi_sparse(self):
636640
with option_context('display.multi_sparse', False):
637641
index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
@@ -1922,15 +1926,195 @@ def test_to_html_index(self):
19221926
'C': ['one', 'two', np.NaN]},
19231927
columns=['A', 'B', 'C'],
19241928
index=index)
1929+
expected_with_index = ('<table border="1" class="dataframe">\n'
1930+
' <thead>\n'
1931+
' <tr style="text-align: right;">\n'
1932+
' <th></th>\n'
1933+
' <th>A</th>\n'
1934+
' <th>B</th>\n'
1935+
' <th>C</th>\n'
1936+
' </tr>\n'
1937+
' </thead>\n'
1938+
' <tbody>\n'
1939+
' <tr>\n'
1940+
' <th>foo</th>\n'
1941+
' <td>1</td>\n'
1942+
' <td>1.2</td>\n'
1943+
' <td>one</td>\n'
1944+
' </tr>\n'
1945+
' <tr>\n'
1946+
' <th>bar</th>\n'
1947+
' <td>2</td>\n'
1948+
' <td>3.4</td>\n'
1949+
' <td>two</td>\n'
1950+
' </tr>\n'
1951+
' <tr>\n'
1952+
' <th>baz</th>\n'
1953+
' <td>3</td>\n'
1954+
' <td>5.6</td>\n'
1955+
' <td>NaN</td>\n'
1956+
' </tr>\n'
1957+
' </tbody>\n'
1958+
'</table>')
1959+
self.assertEqual(df.to_html(), expected_with_index)
1960+
1961+
expected_without_index = ('<table border="1" class="dataframe">\n'
1962+
' <thead>\n'
1963+
' <tr style="text-align: right;">\n'
1964+
' <th>A</th>\n'
1965+
' <th>B</th>\n'
1966+
' <th>C</th>\n'
1967+
' </tr>\n'
1968+
' </thead>\n'
1969+
' <tbody>\n'
1970+
' <tr>\n'
1971+
' <td>1</td>\n'
1972+
' <td>1.2</td>\n'
1973+
' <td>one</td>\n'
1974+
' </tr>\n'
1975+
' <tr>\n'
1976+
' <td>2</td>\n'
1977+
' <td>3.4</td>\n'
1978+
' <td>two</td>\n'
1979+
' </tr>\n'
1980+
' <tr>\n'
1981+
' <td>3</td>\n'
1982+
' <td>5.6</td>\n'
1983+
' <td>NaN</td>\n'
1984+
' </tr>\n'
1985+
' </tbody>\n'
1986+
'</table>')
19251987
result = df.to_html(index=False)
19261988
for i in index:
19271989
self.assertNotIn(i, result)
1990+
self.assertEqual(result, expected_without_index)
1991+
df.index = Index(['foo', 'bar', 'baz'], name='idx')
1992+
expected_with_index = ('<table border="1" class="dataframe">\n'
1993+
' <thead>\n'
1994+
' <tr style="text-align: right;">\n'
1995+
' <th></th>\n'
1996+
' <th>A</th>\n'
1997+
' <th>B</th>\n'
1998+
' <th>C</th>\n'
1999+
' </tr>\n'
2000+
' <tr>\n'
2001+
' <th>idx</th>\n'
2002+
' <th></th>\n'
2003+
' <th></th>\n'
2004+
' <th></th>\n'
2005+
' </tr>\n'
2006+
' </thead>\n'
2007+
' <tbody>\n'
2008+
' <tr>\n'
2009+
' <th>foo</th>\n'
2010+
' <td>1</td>\n'
2011+
' <td>1.2</td>\n'
2012+
' <td>one</td>\n'
2013+
' </tr>\n'
2014+
' <tr>\n'
2015+
' <th>bar</th>\n'
2016+
' <td>2</td>\n'
2017+
' <td>3.4</td>\n'
2018+
' <td>two</td>\n'
2019+
' </tr>\n'
2020+
' <tr>\n'
2021+
' <th>baz</th>\n'
2022+
' <td>3</td>\n'
2023+
' <td>5.6</td>\n'
2024+
' <td>NaN</td>\n'
2025+
' </tr>\n'
2026+
' </tbody>\n'
2027+
'</table>')
2028+
self.assertEqual(df.to_html(), expected_with_index)
2029+
self.assertEqual(df.to_html(index=False), expected_without_index)
19282030

19292031
tuples = [('foo', 'car'), ('foo', 'bike'), ('bar', 'car')]
19302032
df.index = MultiIndex.from_tuples(tuples)
2033+
2034+
expected_with_index = ('<table border="1" class="dataframe">\n'
2035+
' <thead>\n'
2036+
' <tr style="text-align: right;">\n'
2037+
' <th></th>\n'
2038+
' <th></th>\n'
2039+
' <th>A</th>\n'
2040+
' <th>B</th>\n'
2041+
' <th>C</th>\n'
2042+
' </tr>\n'
2043+
' </thead>\n'
2044+
' <tbody>\n'
2045+
' <tr>\n'
2046+
' <th rowspan="2" valign="top">foo</th>\n'
2047+
' <th>car</th>\n'
2048+
' <td>1</td>\n'
2049+
' <td>1.2</td>\n'
2050+
' <td>one</td>\n'
2051+
' </tr>\n'
2052+
' <tr>\n'
2053+
' <th>bike</th>\n'
2054+
' <td>2</td>\n'
2055+
' <td>3.4</td>\n'
2056+
' <td>two</td>\n'
2057+
' </tr>\n'
2058+
' <tr>\n'
2059+
' <th>bar</th>\n'
2060+
' <th>car</th>\n'
2061+
' <td>3</td>\n'
2062+
' <td>5.6</td>\n'
2063+
' <td>NaN</td>\n'
2064+
' </tr>\n'
2065+
' </tbody>\n'
2066+
'</table>')
2067+
self.assertEqual(df.to_html(), expected_with_index)
2068+
19312069
result = df.to_html(index=False)
19322070
for i in ['foo', 'bar', 'car', 'bike']:
19332071
self.assertNotIn(i, result)
2072+
# must be the same result as normal index
2073+
self.assertEqual(result, expected_without_index)
2074+
2075+
df.index = MultiIndex.from_tuples(tuples, names=['idx1', 'idx2'])
2076+
expected_with_index = ('<table border="1" class="dataframe">\n'
2077+
' <thead>\n'
2078+
' <tr style="text-align: right;">\n'
2079+
' <th></th>\n'
2080+
' <th></th>\n'
2081+
' <th>A</th>\n'
2082+
' <th>B</th>\n'
2083+
' <th>C</th>\n'
2084+
' </tr>\n'
2085+
' <tr>\n'
2086+
' <th>idx1</th>\n'
2087+
' <th>idx2</th>\n'
2088+
' <th></th>\n'
2089+
' <th></th>\n'
2090+
' <th></th>\n'
2091+
' </tr>\n'
2092+
' </thead>\n'
2093+
' <tbody>\n'
2094+
' <tr>\n'
2095+
' <th rowspan="2" valign="top">foo</th>\n'
2096+
' <th>car</th>\n'
2097+
' <td>1</td>\n'
2098+
' <td>1.2</td>\n'
2099+
' <td>one</td>\n'
2100+
' </tr>\n'
2101+
' <tr>\n'
2102+
' <th>bike</th>\n'
2103+
' <td>2</td>\n'
2104+
' <td>3.4</td>\n'
2105+
' <td>two</td>\n'
2106+
' </tr>\n'
2107+
' <tr>\n'
2108+
' <th>bar</th>\n'
2109+
' <th>car</th>\n'
2110+
' <td>3</td>\n'
2111+
' <td>5.6</td>\n'
2112+
' <td>NaN</td>\n'
2113+
' </tr>\n'
2114+
' </tbody>\n'
2115+
'</table>')
2116+
self.assertEqual(df.to_html(), expected_with_index)
2117+
self.assertEqual(df.to_html(index=False), expected_without_index)
19342118

19352119
def test_repr_html(self):
19362120
self.frame._repr_html_()

0 commit comments

Comments
 (0)