diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 0d4eaa90d7ab3..2470c04fb97e8 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -777,7 +777,8 @@ I/O - Bug in :func:`read_stata` where the index was not set (:issue:`16342`) - Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`) - Bug in :func:`read_csv` where automatic delimiter detection caused a ``TypeError`` to be thrown when a bad line was encountered rather than the correct error message (:issue:`13374`) -- Bug in ``DataFrame.to_html()`` with ``notebook=True`` where DataFrames with named indices or non-MultiIndex indices had undesired horizontal or vertical alignment for column or row labels, respectively (:issue:`16792`) +- Bug in :meth:`DataFrame.to_html` with ``notebook=True`` where DataFrames with named indices or non-MultiIndex indices had undesired horizontal or vertical alignment for column or row labels, respectively (:issue:`16792`) +- Bug in :meth:`DataFrame.to_html` in which there was no validation of the ``justify`` parameter (:issue:`17527`) - Bug in :func:`HDFStore.select` when reading a contiguous mixed-data table featuring VLArray (:issue:`17021`) Plotting diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 01e83821d4524..778a3dc9046a3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1685,6 +1685,10 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True, .. versionadded:: 0.19.0 """ + if (justify is not None and + justify not in fmt._VALID_JUSTIFY_PARAMETERS): + raise ValueError("Invalid value for justify parameter") + formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns, col_space=col_space, na_rep=na_rep, formatters=formatters, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 386d9c3ffe30d..e8ea0714b1dda 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -79,9 +79,15 @@ line_width : int, optional Width to wrap a line in characters, default no wrap""" +_VALID_JUSTIFY_PARAMETERS = ("left", "right", "center", "justify", + "justify-all", "start", "end", "inherit", + "match-parent", "initial", "unset") + justify_docstring = """ - justify : {'left', 'right'}, default None - Left or right-justify the column labels. If None uses the option from + justify : {'left', 'right', 'center', 'justify', + 'justify-all', 'start', 'end', 'inherit', + 'match-parent', 'initial', 'unset'}, default None + How to justify the column labels. If None uses the option from the print configuration (controlled by set_option), 'right' out of the box.""" diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 194b5ba3e0276..0c8ea98a44d50 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -1557,15 +1557,16 @@ def test_to_html_multiindex(self): assert result == expected - def test_to_html_justify(self): + @pytest.mark.parametrize("justify", fmt._VALID_JUSTIFY_PARAMETERS) + def test_to_html_justify(self, justify): df = DataFrame({'A': [6, 30000, 2], 'B': [1, 2, 70000], 'C': [223442, 0, 1]}, columns=['A', 'B', 'C']) - result = df.to_html(justify='left') + result = df.to_html(justify=justify) expected = ('\n' ' \n' - ' \n' + ' \n' ' \n' ' \n' ' \n' @@ -1592,41 +1593,18 @@ def test_to_html_justify(self): ' \n' ' \n' ' \n' - '
AB1
') + ''.format(justify=justify)) assert result == expected - result = df.to_html(justify='right') - expected = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
061223442
13000020
22700001
') - assert result == expected + @pytest.mark.parametrize("justify", ["super-right", "small-left", + "noinherit", "tiny", "pandas"]) + def test_to_html_invalid_justify(self, justify): + # see gh-17527 + df = DataFrame() + msg = "Invalid value for justify parameter" + + with tm.assert_raises_regex(ValueError, msg): + df.to_html(justify=justify) def test_to_html_index(self): index = ['foo', 'bar', 'baz']