From b8f1c6db532aae51e2ccdc2b14fe9faa27ee2658 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 3 Oct 2017 09:09:36 -0700 Subject: [PATCH 1/4] BUG: Validate the justify parameter in to_html Closes gh-17527. --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/frame.py | 4 +++ pandas/io/formats/format.py | 10 ++++-- pandas/tests/io/formats/test_to_html.py | 48 +++++++------------------ 4 files changed, 26 insertions(+), 37 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 0d4eaa90d7ab3..44113de423029 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -778,6 +778,7 @@ I/O - 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 ``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..f4d97d367ed47 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' @@ -1595,38 +1596,15 @@ def test_to_html_justify(self): '
AB
') 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'] From 3a2f03f8da4f70f2f435e8acec48405828213f7a Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 3 Oct 2017 10:42:50 -0700 Subject: [PATCH 2/4] MAINT: Actually pass in justify to string format --- pandas/tests/io/formats/test_to_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index f4d97d367ed47..9f8b2810fc525 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -1593,7 +1593,7 @@ def test_to_html_justify(self, justify): ' 1\n' ' \n' ' \n' - '') + ''.format(justify=justify) assert result == expected @pytest.mark.parametrize("justify", ["super-right", "small-left", From 072c1b7e3e8ef79b87dfc9fa480ad6d47d12c7d3 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 3 Oct 2017 11:17:12 -0700 Subject: [PATCH 3/4] MAINT: Use method formatting in whatsnew --- doc/source/whatsnew/v0.21.0.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 44113de423029..2470c04fb97e8 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -777,8 +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 ``DataFrame.to_html()`` in which there was no validation of the ``justify`` parameter (:issue:`17527`) +- 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 From 6e43b327df4e0cffceb45a16f4e7ae2cd9476281 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 3 Oct 2017 11:20:06 -0700 Subject: [PATCH 4/4] MAINT: Add missing parenthesis in test_to_html.py --- pandas/tests/io/formats/test_to_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 9f8b2810fc525..0c8ea98a44d50 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -1593,7 +1593,7 @@ def test_to_html_justify(self, justify): ' 1\n' ' \n' ' \n' - ''.format(justify=justify) + ''.format(justify=justify)) assert result == expected @pytest.mark.parametrize("justify", ["super-right", "small-left",