Skip to content

BUG: to_html() with formatters=<list> and max_cols fixed #28183

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 6 commits into from
Sep 16, 2019
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Performance improvements
Bug fixes
~~~~~~~~~

- Bug in :meth:`DataFrame.to_html` when using ``formatters=<list>`` and ``max_cols`` together. (:issue:`25955`)

Categorical
^^^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ def _chk_truncate(self) -> None:
frame = concat(
(frame.iloc[:, :col_num], frame.iloc[:, -col_num:]), axis=1
)
# truncate formatter
if isinstance(self.formatters, (list, tuple)):
truncate_fmt = self.formatters
self.formatters = [
*truncate_fmt[:col_num],
*truncate_fmt[-col_num:],
]
self.tr_col_num = col_num
if truncate_v:
# cast here since if truncate_v is True, max_rows_adj is not None
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/io/formats/data/html/truncate_formatter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>...</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1_mod</td>
<td>...</td>
<td>4</td>
</tr>
<tr>
<th>1</th>
<td>5_mod</td>
<td>...</td>
<td>8</td>
</tr>
<tr>
<th>2</th>
<td>9_mod</td>
<td>...</td>
<td>12</td>
</tr>
<tr>
<th>3</th>
<td>13_mod</td>
<td>...</td>
<td>16</td>
</tr>
</tbody>
</table>
17 changes: 17 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ def test_to_html_truncate(datapath):
assert result == expected


def test_to_html_truncate_formatter(datapath):
# issue-25955
data = [
{"A": 1, "B": 2, "C": 3, "D": 4},
{"A": 5, "B": 6, "C": 7, "D": 8},
{"A": 9, "B": 10, "C": 11, "D": 12},
{"A": 13, "B": 14, "C": 15, "D": 16},
]

df = DataFrame(data)
fmt = lambda x: str(x) + "_mod"
formatters = [fmt, fmt, None, None]
result = df.to_html(formatters=formatters, max_cols=3)
expected = expected_html(datapath, "truncate_formatter")
assert result == expected


@pytest.mark.parametrize(
"sparsify,expected",
[(True, "truncate_multi_index"), (False, "truncate_multi_index_sparse_off")],
Expand Down