From cb145fce2bed8476be2276436e9b466154422add Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Sun, 3 Jul 2022 16:03:52 -0400 Subject: [PATCH 1/3] ENH: Move CSSWarning to error/__init__.py per GH27656 --- doc/source/reference/testing.rst | 1 + pandas/errors/__init__.py | 18 ++++++++++++++++++ pandas/io/formats/css.py | 6 +----- pandas/tests/io/formats/test_css.py | 7 +++---- pandas/tests/io/formats/test_to_excel.py | 2 +- pandas/tests/test_errors.py | 1 + 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/doc/source/reference/testing.rst b/doc/source/reference/testing.rst index c3ce267ff9dc7..249c2c56cfe57 100644 --- a/doc/source/reference/testing.rst +++ b/doc/source/reference/testing.rst @@ -26,6 +26,7 @@ Exceptions and warnings errors.AbstractMethodError errors.AccessorRegistrationWarning + errors.CSSWarning errors.DataError errors.DtypeWarning errors.DuplicateLabelError diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 04d52e2c5853c..73173431a0004 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -395,3 +395,21 @@ def __init__(self, message: str) -> None: # attr only exists on Windows, so typing fails on other platforms message += f" ({ctypes.WinError()})" # type: ignore[attr-defined] super().__init__(message) + + +class CSSWarning(UserWarning): + """ + Warning is raised when trying to convert styling to a css format and it failing. + This can be due to the styling not having an equivalent value or because the + styling isn't properly formatted. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 1, 1]}) + >>> df.style.applymap(lambda x: 'background-color: blueGreenRed;') + ... .to_excel('styled.xlsx') # doctest: +SKIP + ... # CSSWarning: Unhandled color format: 'blueGreenRed' + >>> df.style.applymap(lambda x: 'border: 1px solid red red;') + ... .to_excel('styled.xlsx') # doctest: +SKIP + ... # CSSWarning: Too many tokens provided to "border" (expected 1-3) + """ diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index 92dafffc9c3de..778df087d28d8 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -12,11 +12,7 @@ ) import warnings - -class CSSWarning(UserWarning): - """ - This CSS syntax cannot currently be parsed. - """ +from pandas.errors import CSSWarning def _side_expander(prop_fmt: str) -> Callable: diff --git a/pandas/tests/io/formats/test_css.py b/pandas/tests/io/formats/test_css.py index c93694481ef53..70c91dd02751a 100644 --- a/pandas/tests/io/formats/test_css.py +++ b/pandas/tests/io/formats/test_css.py @@ -1,11 +1,10 @@ import pytest +from pandas.errors import CSSWarning + import pandas._testing as tm -from pandas.io.formats.css import ( - CSSResolver, - CSSWarning, -) +from pandas.io.formats.css import CSSResolver def assert_resolves(css, props, inherited=None): diff --git a/pandas/tests/io/formats/test_to_excel.py b/pandas/tests/io/formats/test_to_excel.py index b98fd74643207..7481baaee94f6 100644 --- a/pandas/tests/io/formats/test_to_excel.py +++ b/pandas/tests/io/formats/test_to_excel.py @@ -6,11 +6,11 @@ import pytest +from pandas.errors import CSSWarning import pandas.util._test_decorators as td import pandas._testing as tm -from pandas.io.formats.css import CSSWarning from pandas.io.formats.excel import ( CssExcelCell, CSSToExcelConverter, diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index e0ce798fec021..177ff566e347a 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -29,6 +29,7 @@ "NumExprClobberingError", "IndexingError", "PyperclipException", + "CSSWarning", ], ) def test_exception_importable(exc): From e3ab8713c51e2bab397a4e2a3a5bde5d0cf98043 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Sun, 3 Jul 2022 18:18:25 -0400 Subject: [PATCH 2/3] ENH: fix typo --- pandas/errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 73173431a0004..9eca1c878fa61 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -399,7 +399,7 @@ def __init__(self, message: str) -> None: class CSSWarning(UserWarning): """ - Warning is raised when trying to convert styling to a css format and it failing. + Warning is raised when trying to convert styling to a css format and it fails. This can be due to the styling not having an equivalent value or because the styling isn't properly formatted. From 1dec69aae2e080fceb27c76960dc72743a57ce40 Mon Sep 17 00:00:00 2001 From: Derek Sharp Date: Sun, 3 Jul 2022 18:49:35 -0400 Subject: [PATCH 3/3] ENH: reword sentence --- pandas/errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 9eca1c878fa61..98a9d2b35f09d 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -399,7 +399,7 @@ def __init__(self, message: str) -> None: class CSSWarning(UserWarning): """ - Warning is raised when trying to convert styling to a css format and it fails. + Warning is raised when converting css styling fails. This can be due to the styling not having an equivalent value or because the styling isn't properly formatted.