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..98a9d2b35f09d 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 converting css styling fails. + 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):