From dfdb25bdd4e6514dffdf6adf371fc728f589e6cc Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 3 Feb 2022 20:39:22 +0100 Subject: [PATCH 1/2] fix pd.NA in highlighters --- pandas/io/formats/style.py | 8 +++++++- pandas/tests/io/formats/style/test_highlight.py | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index f97f558fd0e0b..8e3e35a510c58 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3710,11 +3710,15 @@ def _highlight_between( if left is not None else np.full(data.shape, True, dtype=bool) ) + if isinstance(g_left, (DataFrame, Series)): + g_left = g_left.where(pd.notna(g_left), False) l_right = ( ops[1](data, right) if right is not None else np.full(data.shape, True, dtype=bool) ) + if isinstance(l_right, (DataFrame, Series)): + l_right = l_right.where(pd.notna(l_right), False) return np.where(g_left & l_right, props, "") @@ -3725,7 +3729,9 @@ def _highlight_value(data: DataFrame | Series, op: str, props: str) -> np.ndarra value = getattr(data, op)(skipna=True) if isinstance(data, DataFrame): # min/max must be done twice to return scalar value = getattr(value, op)(skipna=True) - return np.where(data == value, props, "") + cond = data == value + cond = cond.where(pd.notna(cond), False) + return np.where(cond, props, "") def _bar( diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index 1b579a43370a2..89aa0686562a9 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -12,9 +12,12 @@ from pandas.io.formats.style import Styler -@pytest.fixture -def df(): - return DataFrame({"A": [0, np.nan, 10], "B": [1, None, 2]}) +@pytest.fixture(params=[(None, "float64"), (NA, "Int64")]) +def df(request): + # GH 45804 + return DataFrame( + {"A": [0, np.nan, 10], "B": [1, request.param[0], 2]}, dtype=request.param[1] + ) @pytest.fixture From 5b6515bbefabf99a50a4a30efff30f3e5981598e Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 4 Feb 2022 18:15:43 +0100 Subject: [PATCH 2/2] whats new --- doc/source/whatsnew/v1.4.1.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 3dd0fcdb5b87d..f74215f1f0361 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -33,6 +33,7 @@ Bug fixes - Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`) - Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`) - Bug in :func:`api.types.is_bool_dtype` was raising an ``AttributeError`` when evaluating a categorical :class:`Series` (:issue:`45615`) +- Fixed builtin highlighters in :class:`.Styler` to be responsive to ``NA`` with nullable dtypes (:issue:`45804`) .. ---------------------------------------------------------------------------