Skip to content

bug: fix pd.NA in highlighters #45808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")


Expand All @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/io/formats/style/test_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down