From d7165efd2269bdd39c1a3c2e17630384e5020f9b Mon Sep 17 00:00:00 2001 From: ArtificialQualia Date: Sat, 9 Mar 2019 11:51:18 -0500 Subject: [PATCH 1/3] BUG: Fix user-facing AssertionError with to_html (#25608) --- doc/source/whatsnew/v0.24.2.rst | 2 +- pandas/io/formats/html.py | 4 ++-- pandas/tests/io/formats/test_to_html.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index 2c6d1e01ed89b..abf09f16568f4 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -64,7 +64,7 @@ Bug Fixes - Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`) - Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`) - Bug where float indexes could have misaligned values when printing (:issue:`25061`) -- +- Bug in converting to HTML when using an invalid type for ``classes`` parameter raised ``AsseertionError`` instead of ``TypeError`` (:issue:`25608`) **Categorical** diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 66d13bf2668f9..5d6fb8c980f61 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -163,8 +163,8 @@ def _write_table(self, indent=0): if isinstance(self.classes, str): self.classes = self.classes.split() if not isinstance(self.classes, (list, tuple)): - raise AssertionError('classes must be list or tuple, not {typ}' - .format(typ=type(self.classes))) + raise TypeError('classes must be a string, list or tuple, ' + 'not {typ}'.format(typ=type(self.classes))) _classes.extend(self.classes) if self.table_id is None: diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 428f1411a10a6..b1ebd3acd808b 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -623,3 +623,13 @@ def test_ignore_display_max_colwidth(method, expected, max_colwidth): result = getattr(df, method)() expected = expected(max_colwidth) assert expected in result + + +@pytest.mark.parametrize("classes", [True, 0]) +def test_to_html_invalid_classes_type(classes): + # GH 25608 + df = DataFrame() + msg = "classes must be a string, list or tuple" + + with pytest.raises(TypeError, match=msg): + df.to_html(classes=classes) From 1c72638cf2ff8f92fef1c1038cbeb1aa6207ef7c Mon Sep 17 00:00:00 2001 From: ArtificialQualia Date: Sat, 9 Mar 2019 13:26:22 -0500 Subject: [PATCH 2/3] added oxford comma, moved whatsnew entry to 0.25.0 --- doc/source/whatsnew/v0.24.2.rst | 2 +- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/io/formats/html.py | 2 +- pandas/tests/io/formats/test_to_html.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index abf09f16568f4..2c6d1e01ed89b 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -64,7 +64,7 @@ Bug Fixes - Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`) - Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`) - Bug where float indexes could have misaligned values when printing (:issue:`25061`) -- Bug in converting to HTML when using an invalid type for ``classes`` parameter raised ``AsseertionError`` instead of ``TypeError`` (:issue:`25608`) +- **Categorical** diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index ea08a0a6fe07b..ecc6d9a4453a6 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -214,7 +214,7 @@ I/O - Bug in :func:`read_json` for ``orient='table'`` when it tries to infer dtypes by default, which is not applicable as dtypes are already defined in the JSON schema (:issue:`21345`) - Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`) - Bug in :func:`read_json` for ``orient='table'`` and string of float column names, as it makes a column name type conversion to Timestamp, which is not applicable because column names are already defined in the JSON schema (:issue:`25435`) -- +- Bug in converting to HTML when using an invalid type for ``classes`` parameter raised ``AsseertionError`` instead of ``TypeError`` (:issue:`25608`) - - diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py index 5d6fb8c980f61..a543b21f287ec 100644 --- a/pandas/io/formats/html.py +++ b/pandas/io/formats/html.py @@ -163,7 +163,7 @@ def _write_table(self, indent=0): if isinstance(self.classes, str): self.classes = self.classes.split() if not isinstance(self.classes, (list, tuple)): - raise TypeError('classes must be a string, list or tuple, ' + raise TypeError('classes must be a string, list, or tuple, ' 'not {typ}'.format(typ=type(self.classes))) _classes.extend(self.classes) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index b1ebd3acd808b..9cb2704f65587 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -629,7 +629,7 @@ def test_ignore_display_max_colwidth(method, expected, max_colwidth): def test_to_html_invalid_classes_type(classes): # GH 25608 df = DataFrame() - msg = "classes must be a string, list or tuple" + msg = "classes must be a string, list, or tuple" with pytest.raises(TypeError, match=msg): df.to_html(classes=classes) From 47399658af245242d5c73f1ba4e8c87f867e386b Mon Sep 17 00:00:00 2001 From: Kendall Masse Date: Sun, 10 Mar 2019 14:12:49 -0400 Subject: [PATCH 3/3] update whatsnew entry --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index ecc6d9a4453a6..f5fa7a71e117c 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -214,7 +214,7 @@ I/O - Bug in :func:`read_json` for ``orient='table'`` when it tries to infer dtypes by default, which is not applicable as dtypes are already defined in the JSON schema (:issue:`21345`) - Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`) - Bug in :func:`read_json` for ``orient='table'`` and string of float column names, as it makes a column name type conversion to Timestamp, which is not applicable because column names are already defined in the JSON schema (:issue:`25435`) -- Bug in converting to HTML when using an invalid type for ``classes`` parameter raised ``AsseertionError`` instead of ``TypeError`` (:issue:`25608`) +- :meth:`DataFrame.to_html` now raises ``TypeError`` when using an invalid type for the ``classes`` parameter instead of ``AsseertionError`` (:issue:`25608`) - -