Skip to content

BUG: DataFrame.to_html() fails when index=False and max_rows is specified #8290

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
wants to merge 3 commits into from
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
24 changes: 17 additions & 7 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,17 +937,20 @@ def _write_body(self, indent):
else:
self._write_regular_rows(fmt_values, indent)
else:
for i in range(len(self.frame)):
row = [fmt_values[j][i] for j in range(len(self.columns))]
self.write_tr(row, indent, self.indent_delta, tags=None)
if isinstance(self.frame.index, MultiIndex):
for i in range(len(self.frame)):
row = [fmt_values[j][i] for j in range(len(self.columns))]
self.write_tr(row, indent, self.indent_delta, tags=None)
else:
self._write_regular_rows(fmt_values, indent, include_index=False)

indent -= self.indent_delta
self.write('</tbody>', indent)
indent -= self.indent_delta

return indent

def _write_regular_rows(self, fmt_values, indent):
def _write_regular_rows(self, fmt_values, indent, include_index=True):
truncate_h = self.fmt.truncate_h
truncate_v = self.fmt.truncate_v

Expand All @@ -959,22 +962,29 @@ def _write_regular_rows(self, fmt_values, indent):
else:
index_values = self.fmt.tr_frame.index.format()

if include_index:
nindex_levels = 1
else:
nindex_levels = 0

for i in range(nrows):

if truncate_v and i == (self.fmt.tr_row_num):
str_sep_row = [ '...' for ele in row ]
self.write_tr(str_sep_row, indent, self.indent_delta, tags=None,
nindex_levels=1)
nindex_levels=nindex_levels)

row = []
row.append(index_values[i])
if include_index:
row.append(index_values[i])
row.extend(fmt_values[j][i] for j in range(ncols))

if truncate_h:
dot_col_ix = self.fmt.tr_col_num + 1
row.insert(dot_col_ix, '...')

self.write_tr(row, indent, self.indent_delta, tags=None,
nindex_levels=1)
nindex_levels=nindex_levels)

def _write_hierarchical_rows(self, fmt_values, indent):
template = 'rowspan="%d" valign="top"'
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,36 @@ def test_to_html_truncate(self):
expected = expected.decode('utf-8')
self.assertEqual(result, expected)

def test_to_html_truncate_no_index(self):
df = pd.DataFrame({'a': np.arange(0, 10), 'b': np.arange(9, -1, -1)})
result = df.to_html(index=False, max_rows=3)
expected = '''\
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td> 0</td>
<td> 9</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td> 9</td>
<td> 0</td>
</tr>
</tbody>
</table>'''
if sys.version_info[0] < 3:
expected = expected.decode('utf-8')
self.assertEqual(result, expected)

def test_to_html_truncate_multi_index(self):
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
Expand Down