Skip to content

BUG: Fixed to_html with index=False and max_rows #14999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ Bug Fixes



- Bug in ``DataFrame.to_html`` with ``index=False`` and ``max_rows`` raising in ``IndexError`` (:issue:`14998`)








Expand Down
2 changes: 1 addition & 1 deletion pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def _write_body(self, indent):
else:
self._write_regular_rows(fmt_values, indent)
else:
for i in range(len(self.frame)):
for i in range(min(len(self.frame), self.max_rows)):
row = [fmt_values[j][i] for j in range(len(self.columns))]
self.write_tr(row, indent, self.indent_delta, tags=None)

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,25 @@ def test_to_html_with_classes(self):
result = df.to_html(classes=["sortable", "draggable"])
self.assertEqual(result, expected)

def test_to_html_no_index_max_rows(self):
# GH https://github.com/pandas-dev/pandas/issues/14998
df = DataFrame({"A": [1, 2, 3, 4]})
result = df.to_html(index=False, max_rows=1)
expected = dedent("""\
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>""")
self.assertEqual(result, expected)

def test_pprint_pathological_object(self):
"""
if the test fails, the stack will overflow and nose crash,
Expand Down