Skip to content

Commit 2bd8629

Browse files
committed
BUG: Fixed to_html with index=False and max_rows
closes #14998 Previously raised an IndexError by assuming that `len(fmt_values)` was always the same length as `len(self.data)`.
1 parent 7f0eefc commit 2bd8629

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ Bug Fixes
313313

314314

315315

316+
- Bug in ``DataFrame.to_html`` with ``index=False`` and ``max_rows`` raising in ``IndexError`` (:issue:`14998`)
317+
318+
319+
320+
321+
316322

317323

318324

pandas/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ def _write_body(self, indent):
11821182
else:
11831183
self._write_regular_rows(fmt_values, indent)
11841184
else:
1185-
for i in range(len(self.frame)):
1185+
for i in range(min(len(self.frame), self.max_rows)):
11861186
row = [fmt_values[j][i] for j in range(len(self.columns))]
11871187
self.write_tr(row, indent, self.indent_delta, tags=None)
11881188

pandas/tests/formats/test_format.py

+19
Original file line numberDiff line numberDiff line change
@@ -2771,6 +2771,25 @@ def test_to_html_with_classes(self):
27712771
result = df.to_html(classes=["sortable", "draggable"])
27722772
self.assertEqual(result, expected)
27732773

2774+
def test_to_html_no_index_max_rows(self):
2775+
# GH https://github.com/pandas-dev/pandas/issues/14998
2776+
df = DataFrame({"A": [1, 2, 3, 4]})
2777+
result = df.to_html(index=False, max_rows=1)
2778+
expected = dedent("""\
2779+
<table border="1" class="dataframe">
2780+
<thead>
2781+
<tr style="text-align: right;">
2782+
<th>A</th>
2783+
</tr>
2784+
</thead>
2785+
<tbody>
2786+
<tr>
2787+
<td>1</td>
2788+
</tr>
2789+
</tbody>
2790+
</table>""")
2791+
self.assertEqual(result, expected)
2792+
27742793
def test_pprint_pathological_object(self):
27752794
"""
27762795
if the test fails, the stack will overflow and nose crash,

0 commit comments

Comments
 (0)