Skip to content

BUG: to_html misses truncation indicators (...) when index=False #22786

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

Merged
merged 12 commits into from
Nov 15, 2018
1 change: 1 addition & 0 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def _write_body(self, indent):
self._write_hierarchical_rows(fmt_values, indent)
else:
self._write_regular_rows(fmt_values, indent)
# GH 15019, GH 22783 add truncation logic below
else:
for i in range(min(len(self.frame), self.max_rows)):
row = [fmt_values[j][i] for j in range(len(self.columns))]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/formats/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest


@pytest.fixture
def read_file(datapath):
"""fixture factory to read text files from tests/io/formats/data"""
def _read_file(filename):
filepath = datapath('io', 'formats', 'data', filename)
with open(filepath) as f:
contents = f.read()
return contents
return _read_file
31 changes: 31 additions & 0 deletions pandas/tests/io/formats/data/gh15019_expected_output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>0</th>
<th>1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.764052</td>
<td>0.400157</td>
</tr>
<tr>
<td>0.978738</td>
<td>2.240893</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>0.950088</td>
<td>-0.151357</td>
</tr>
<tr>
<td>-0.103219</td>
<td>0.410599</td>
</tr>
</tbody>
</table>

27 changes: 27 additions & 0 deletions pandas/tests/io/formats/data/gh22783_expected_output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>0</th>
<th>1</th>
<th>...</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.764052</td>
<td>0.400157</td>
<td>...</td>
<td>2.240893</td>
<td>1.867558</td>
</tr>
<tr>
<td>-0.977278</td>
<td>0.950088</td>
<td>...</td>
<td>-0.103219</td>
<td>0.410599</td>
</tr>
</tbody>
</table>
25 changes: 25 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
pass


@pytest.fixture
def expected_html(read_file):
def _expected_html(name):
filename = '.'.join([name, 'html'])
html = read_file(filename)
return html.rstrip()
return _expected_html


class TestToHTML(object):

def test_to_html_with_col_space(self):
Expand Down Expand Up @@ -1905,6 +1914,22 @@ def test_to_html_multiindex_max_cols(self):
</table>""")
assert result == expected

def test_to_html_truncation_index_false_max_rows(self, expected_html):
# GH 15019
np.random.seed(seed=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do really need to set the seed?

df = pd.DataFrame(np.random.randn(5, 2))
result = df.to_html(max_rows=4, index=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize over index=False and index=0

expected = expected_html('gh15019_expected_output')
assert result == expected

def test_to_html_truncation_index_false_max_cols(self, expected_html):
# GH 22783
np.random.seed(seed=0)
df = pd.DataFrame(np.random.randn(2, 5))
result = df.to_html(max_cols=4, index=False)
expected = expected_html('gh22783_expected_output')
assert result == expected

def test_to_html_notebook_has_style(self):
df = pd.DataFrame({"A": [1, 2, 3]})
result = df.to_html(notebook=True)
Expand Down