diff --git a/pandas/core/format.py b/pandas/core/format.py
index fa2135bb4310c..ecce3b54239c9 100644
--- a/pandas/core/format.py
+++ b/pandas/core/format.py
@@ -736,7 +736,7 @@ def _write_hierarchical_rows(self, fmt_values, indent):
row.extend(idx_values[i])
row.extend(fmt_values[j][i] for j in range(ncols))
self.write_tr(row, indent, self.indent_delta, tags=None,
- nindex_levels=len(frame.index.nlevels))
+ nindex_levels=frame.index.nlevels)
def _get_level_lengths(levels):
diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py
index 37f08dd177eae..89988a21894b9 100644
--- a/pandas/tests/test_format.py
+++ b/pandas/tests/test_format.py
@@ -435,6 +435,114 @@ def test_to_html_escape_disabled(self):
"""
self.assertEqual(xp, rs)
+ 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]],
+ names=['foo', None])
+
+ df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index)
+
+ result = df.to_html()
+ expected = """\
+
+
+
+ |
+ |
+ 0 |
+ 1 |
+
+
+ foo |
+ |
+ |
+ |
+
+
+
+
+ 0 |
+ 0 |
+ 0 |
+ 1 |
+
+
+ 0 |
+ 1 |
+ 2 |
+ 3 |
+
+
+ 1 |
+ 0 |
+ 4 |
+ 5 |
+
+
+ 1 |
+ 1 |
+ 6 |
+ 7 |
+
+
+
"""
+ self.assertEquals(result, expected)
+
+ df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]],
+ columns=index[::2], index=index)
+
+ result = df.to_html()
+ expected = """\
+
+
+
+ |
+ foo |
+ 0 |
+ 1 |
+
+
+ |
+ |
+ 0 |
+ 0 |
+
+
+ foo |
+ |
+ |
+ |
+
+
+
+
+ 0 |
+ 0 |
+ 0 |
+ 1 |
+
+
+ 0 |
+ 1 |
+ 2 |
+ 3 |
+
+
+ 1 |
+ 0 |
+ 4 |
+ 5 |
+
+
+ 1 |
+ 1 |
+ 6 |
+ 7 |
+
+
+
"""
+ self.assertEquals(result, expected)
+
def test_to_html_multiindex_sparsify(self):
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
names=['foo', None])