Skip to content

BUG: DataFrame.to_html validates formatters has the correct length #28632

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 2 commits into from
Oct 7, 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 @@ -252,6 +252,7 @@ I/O
- Bug in :func:`DataFrame.to_string` where values were truncated using display options instead of outputting the full content (:issue:`9784`)
- Bug in :meth:`DataFrame.to_json` where a datetime column label would not be written out in ISO format with ``orient="table"`` (:issue:`28130`)
- Bug in :func:`DataFrame.to_parquet` where writing to GCS would fail with `engine='fastparquet'` if the file did not already exist (:issue:`28326`)
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)

Plotting
^^^^^^^^
Expand Down
12 changes: 11 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,17 @@ def __init__(
self.sparsify = sparsify

self.float_format = float_format
self.formatters = formatters if formatters is not None else {}
if formatters is None:
self.formatters = {}
elif len(frame.columns) == len(formatters) or isinstance(formatters, dict):
self.formatters = formatters
else:
raise ValueError(
(
"Formatters length({flen}) should match"
" DataFrame number of columns({dlen})"
).format(flen=len(formatters), dlen=len(frame.columns))
)
Copy link
Contributor

Choose a reason for hiding this comment

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

this logic is quite complicated, can you not do

if formaters is not None and not do_len_comparision:
     raise....
self.formatters = formatters or {}

?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better now?

self.na_rep = na_rep
self.decimal = decimal
self.col_space = col_space
Expand Down
9 changes: 9 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,15 @@ def test_to_html_truncate(datapath):
assert result == expected


@pytest.mark.parametrize("size", [1, 5])
def test_html_invalid_formatters_arg_raises(size):
# issue-28469
df = DataFrame(columns=["a", "b", "c"])
msg = "Formatters length({}) should match DataFrame number of columns(3)"
with pytest.raises(ValueError, match=re.escape(msg.format(size))):
df.to_html(formatters=["{}".format] * size)


def test_to_html_truncate_formatter(datapath):
# issue-25955
data = [
Expand Down