Skip to content

Commit 89f7039

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
BUG: TypeError with to_html(sparsify=False) and max_cols < len(columns) (pandas-dev#24572)
1 parent 72c8401 commit 89f7039

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
16041604
- Bug in :func:`to_html()` with ``index=False`` when both columns and row index are ``MultiIndex`` (:issue:`22579`)
16051605
- Bug in :func:`to_html()` with ``index_names=False`` displaying index name (:issue:`22747`)
16061606
- Bug in :func:`to_html()` with ``header=False`` not displaying row index names (:issue:`23788`)
1607+
- Bug in :func:`to_html()` with ``sparsify=False`` that caused it to raise ``TypeError`` (:issue:`22887`)
16071608
- Bug in :func:`DataFrame.to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`)
16081609
- Bug in :func:`DataFrame.to_string()` that caused representations of :class:`DataFrame` to not take up the whole window (:issue:`22984`)
16091610
- Bug in :func:`DataFrame.to_csv` where a single level MultiIndex incorrectly wrote a tuple. Now just the value of the index is written (:issue:`19589`).

pandas/io/formats/html.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def _write_col_header(self, indent):
241241
# GH3547
242242
sentinel = com.sentinel_factory()
243243
else:
244-
sentinel = None
244+
sentinel = False
245245
levels = self.columns.format(sparsify=sentinel, adjoin=False,
246246
names=False)
247247
level_lengths = get_level_lengths(levels, sentinel)
@@ -440,9 +440,6 @@ def _write_hierarchical_rows(self, fmt_values, indent):
440440
truncate_v = self.fmt.truncate_v
441441
frame = self.fmt.tr_frame
442442
nrows = len(frame)
443-
# TODO: after gh-22887 fixed, refactor to use class property
444-
# in place of row_levels
445-
row_levels = self.frame.index.nlevels
446443

447444
idx_values = frame.index.format(sparsify=False, adjoin=False,
448445
names=False)
@@ -520,18 +517,24 @@ def _write_hierarchical_rows(self, fmt_values, indent):
520517

521518
row.extend(fmt_values[j][i] for j in range(self.ncols))
522519
if truncate_h:
523-
row.insert(row_levels - sparse_offset +
520+
row.insert(self.row_levels - sparse_offset +
524521
self.fmt.tr_col_num, '...')
525522
self.write_tr(row, indent, self.indent_delta, tags=tags,
526523
nindex_levels=len(levels) - sparse_offset)
527524
else:
525+
row = []
528526
for i in range(len(frame)):
527+
if truncate_v and i == (self.fmt.tr_row_num):
528+
str_sep_row = ['...'] * len(row)
529+
self.write_tr(str_sep_row, indent, self.indent_delta,
530+
tags=None, nindex_levels=self.row_levels)
531+
529532
idx_values = list(zip(*frame.index.format(
530533
sparsify=False, adjoin=False, names=False)))
531534
row = []
532535
row.extend(idx_values[i])
533536
row.extend(fmt_values[j][i] for j in range(self.ncols))
534537
if truncate_h:
535-
row.insert(row_levels + self.fmt.tr_col_num, '...')
538+
row.insert(self.row_levels + self.fmt.tr_col_num, '...')
536539
self.write_tr(row, indent, self.indent_delta, tags=None,
537540
nindex_levels=frame.index.nlevels)

pandas/tests/io/formats/data/html/truncate_multi_index_sparse_off.html

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
<td>NaN</td>
5858
<td>NaN</td>
5959
</tr>
60+
<tr>
61+
<th>...</th>
62+
<th>...</th>
63+
<td>...</td>
64+
<td>...</td>
65+
<td>...</td>
66+
<td>...</td>
67+
<td>...</td>
68+
<td>...</td>
69+
<td>...</td>
70+
</tr>
6071
<tr>
6172
<th>foo</th>
6273
<th>two</th>

pandas/tests/io/formats/test_to_html.py

-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ def test_to_html_truncate_multi_index(self, datapath):
223223
expected = expected_html(datapath, 'truncate_multi_index')
224224
assert result == expected
225225

226-
@pytest.mark.xfail(reason='GH22887 TypeError')
227226
def test_to_html_truncate_multi_index_sparse_off(self, datapath):
228227
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
229228
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]

0 commit comments

Comments
 (0)