Skip to content

Commit d6baad8

Browse files
Backport PR #45808: bug: fix pd.NA in highlighters (#45828)
Co-authored-by: JHM Darbyshire <[email protected]>
1 parent 75370d5 commit d6baad8

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
@@ -33,6 +33,7 @@ Bug fixes
3333
- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`)
3434
- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
3535
- Bug in :func:`api.types.is_bool_dtype` was raising an ``AttributeError`` when evaluating a categorical :class:`Series` (:issue:`45615`)
36+
- Fixed builtin highlighters in :class:`.Styler` to be responsive to ``NA`` with nullable dtypes (:issue:`45804`)
3637

3738
.. ---------------------------------------------------------------------------
3839

pandas/io/formats/style.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3640,11 +3640,15 @@ def _highlight_between(
36403640
if left is not None
36413641
else np.full(data.shape, True, dtype=bool)
36423642
)
3643+
if isinstance(g_left, (DataFrame, Series)):
3644+
g_left = g_left.where(pd.notna(g_left), False)
36433645
l_right = (
36443646
ops[1](data, right)
36453647
if right is not None
36463648
else np.full(data.shape, True, dtype=bool)
36473649
)
3650+
if isinstance(l_right, (DataFrame, Series)):
3651+
l_right = l_right.where(pd.notna(l_right), False)
36483652
return np.where(g_left & l_right, props, "")
36493653

36503654

@@ -3655,7 +3659,9 @@ def _highlight_value(data: DataFrame | Series, op: str, props: str) -> np.ndarra
36553659
value = getattr(data, op)(skipna=True)
36563660
if isinstance(data, DataFrame): # min/max must be done twice to return scalar
36573661
value = getattr(value, op)(skipna=True)
3658-
return np.where(data == value, props, "")
3662+
cond = data == value
3663+
cond = cond.where(pd.notna(cond), False)
3664+
return np.where(cond, props, "")
36593665

36603666

36613667
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)