Skip to content

Commit 46aac95

Browse files
author
y-p
committed
Merge pull request #3557 from cpcloud/html-typeerror-bug-fix
fix TypeError for display.multi_sparse == False
2 parents 4670e9f + 85a7ebd commit 46aac95

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

pandas/core/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def _write_hierarchical_rows(self, fmt_values, indent):
736736
row.extend(idx_values[i])
737737
row.extend(fmt_values[j][i] for j in range(ncols))
738738
self.write_tr(row, indent, self.indent_delta, tags=None,
739-
nindex_levels=len(frame.index.nlevels))
739+
nindex_levels=frame.index.nlevels)
740740

741741

742742
def _get_level_lengths(levels):

pandas/tests/test_format.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,114 @@ def test_to_html_escape_disabled(self):
435435
</table>"""
436436
self.assertEqual(xp, rs)
437437

438+
def test_to_html_multiindex_sparsify_false_multi_sparse(self):
439+
with option_context('display.multi_sparse', False):
440+
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
441+
names=['foo', None])
442+
443+
df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index)
444+
445+
result = df.to_html()
446+
expected = """\
447+
<table border="1" class="dataframe">
448+
<thead>
449+
<tr style="text-align: right;">
450+
<th></th>
451+
<th></th>
452+
<th>0</th>
453+
<th>1</th>
454+
</tr>
455+
<tr>
456+
<th>foo</th>
457+
<th></th>
458+
<th></th>
459+
<th></th>
460+
</tr>
461+
</thead>
462+
<tbody>
463+
<tr>
464+
<th>0</th>
465+
<th>0</th>
466+
<td> 0</td>
467+
<td> 1</td>
468+
</tr>
469+
<tr>
470+
<th>0</th>
471+
<th>1</th>
472+
<td> 2</td>
473+
<td> 3</td>
474+
</tr>
475+
<tr>
476+
<th>1</th>
477+
<th>0</th>
478+
<td> 4</td>
479+
<td> 5</td>
480+
</tr>
481+
<tr>
482+
<th>1</th>
483+
<th>1</th>
484+
<td> 6</td>
485+
<td> 7</td>
486+
</tr>
487+
</tbody>
488+
</table>"""
489+
self.assertEquals(result, expected)
490+
491+
df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]],
492+
columns=index[::2], index=index)
493+
494+
result = df.to_html()
495+
expected = """\
496+
<table border="1" class="dataframe">
497+
<thead>
498+
<tr>
499+
<th></th>
500+
<th>foo</th>
501+
<th>0</th>
502+
<th>1</th>
503+
</tr>
504+
<tr>
505+
<th></th>
506+
<th></th>
507+
<th>0</th>
508+
<th>0</th>
509+
</tr>
510+
<tr>
511+
<th>foo</th>
512+
<th></th>
513+
<th></th>
514+
<th></th>
515+
</tr>
516+
</thead>
517+
<tbody>
518+
<tr>
519+
<th>0</th>
520+
<th>0</th>
521+
<td> 0</td>
522+
<td> 1</td>
523+
</tr>
524+
<tr>
525+
<th>0</th>
526+
<th>1</th>
527+
<td> 2</td>
528+
<td> 3</td>
529+
</tr>
530+
<tr>
531+
<th>1</th>
532+
<th>0</th>
533+
<td> 4</td>
534+
<td> 5</td>
535+
</tr>
536+
<tr>
537+
<th>1</th>
538+
<th>1</th>
539+
<td> 6</td>
540+
<td> 7</td>
541+
</tr>
542+
</tbody>
543+
</table>"""
544+
self.assertEquals(result, expected)
545+
438546
def test_to_html_multiindex_sparsify(self):
439547
index = pd.MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]],
440548
names=['foo', None])

0 commit comments

Comments
 (0)