Skip to content

Commit 9a29ba1

Browse files
authored
bug: fix pd.NA in highlighters (#45808)
1 parent 357845d commit 9a29ba1

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

doc/source/whatsnew/v1.4.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Bug fixes
3434
- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`)
3535
- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
3636
- Bug in :func:`api.types.is_bool_dtype` was raising an ``AttributeError`` when evaluating a categorical :class:`Series` (:issue:`45615`)
37+
- Fixed builtin highlighters in :class:`.Styler` to be responsive to ``NA`` with nullable dtypes (:issue:`45804`)
3738

3839
.. ---------------------------------------------------------------------------
3940

pandas/io/formats/style.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3710,11 +3710,15 @@ def _highlight_between(
37103710
if left is not None
37113711
else np.full(data.shape, True, dtype=bool)
37123712
)
3713+
if isinstance(g_left, (DataFrame, Series)):
3714+
g_left = g_left.where(pd.notna(g_left), False)
37133715
l_right = (
37143716
ops[1](data, right)
37153717
if right is not None
37163718
else np.full(data.shape, True, dtype=bool)
37173719
)
3720+
if isinstance(l_right, (DataFrame, Series)):
3721+
l_right = l_right.where(pd.notna(l_right), False)
37183722
return np.where(g_left & l_right, props, "")
37193723

37203724

@@ -3725,7 +3729,9 @@ def _highlight_value(data: DataFrame | Series, op: str, props: str) -> np.ndarra
37253729
value = getattr(data, op)(skipna=True)
37263730
if isinstance(data, DataFrame): # min/max must be done twice to return scalar
37273731
value = getattr(value, op)(skipna=True)
3728-
return np.where(data == value, props, "")
3732+
cond = data == value
3733+
cond = cond.where(pd.notna(cond), False)
3734+
return np.where(cond, props, "")
37293735

37303736

37313737
def _bar(

pandas/tests/io/formats/style/test_highlight.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
from pandas.io.formats.style import Styler
1313

1414

15-
@pytest.fixture
16-
def df():
17-
return DataFrame({"A": [0, np.nan, 10], "B": [1, None, 2]})
15+
@pytest.fixture(params=[(None, "float64"), (NA, "Int64")])
16+
def df(request):
17+
# GH 45804
18+
return DataFrame(
19+
{"A": [0, np.nan, 10], "B": [1, request.param[0], 2]}, dtype=request.param[1]
20+
)
1821

1922

2023
@pytest.fixture

0 commit comments

Comments
 (0)