Skip to content

Commit a77ad8b

Browse files
authored
ENH: Styler.highlight_null can accepts subset argument (pandas-dev#31350)
1 parent 37659d4 commit a77ad8b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Other enhancements
6464
^^^^^^^^^^^^^^^^^^
6565

6666
- :class:`Styler` may now render CSS more efficiently where multiple cells have the same styling (:issue:`30876`)
67+
- :meth:`Styler.highlight_null` now accepts ``subset`` argument (:issue:`31345`)
6768
- When writing directly to a sqlite connection :func:`to_sql` now supports the ``multi`` method (:issue:`29921`)
6869
- `OptionError` is now exposed in `pandas.errors` (:issue:`27553`)
6970
- :func:`timedelta_range` will now infer a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`)

pandas/io/formats/style.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1003,19 +1003,27 @@ def hide_columns(self, subset) -> "Styler":
10031003
def _highlight_null(v, null_color: str) -> str:
10041004
return f"background-color: {null_color}" if pd.isna(v) else ""
10051005

1006-
def highlight_null(self, null_color: str = "red") -> "Styler":
1006+
def highlight_null(
1007+
self,
1008+
null_color: str = "red",
1009+
subset: Optional[Union[Label, Sequence[Label]]] = None,
1010+
) -> "Styler":
10071011
"""
10081012
Shade the background ``null_color`` for missing values.
10091013
10101014
Parameters
10111015
----------
1012-
null_color : str
1016+
null_color : str, default 'red'
1017+
subset : label or list of labels, default None
1018+
A valid slice for ``data`` to limit the style application to.
1019+
1020+
.. versionadded:: 1.1.0
10131021
10141022
Returns
10151023
-------
10161024
self : Styler
10171025
"""
1018-
self.applymap(self._highlight_null, null_color=null_color)
1026+
self.applymap(self._highlight_null, null_color=null_color, subset=subset)
10191027
return self
10201028

10211029
def background_gradient(

pandas/tests/io/formats/test_style.py

+17
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,23 @@ def test_highlight_null(self, null_color="red"):
10911091
expected = {(0, 0): [""], (1, 0): ["background-color: red"]}
10921092
assert result == expected
10931093

1094+
def test_highlight_null_subset(self):
1095+
# GH 31345
1096+
df = pd.DataFrame({"A": [0, np.nan], "B": [0, np.nan]})
1097+
result = (
1098+
df.style.highlight_null(null_color="red", subset=["A"])
1099+
.highlight_null(null_color="green", subset=["B"])
1100+
._compute()
1101+
.ctx
1102+
)
1103+
expected = {
1104+
(0, 0): [""],
1105+
(1, 0): ["background-color: red"],
1106+
(0, 1): [""],
1107+
(1, 1): ["background-color: green"],
1108+
}
1109+
assert result == expected
1110+
10941111
def test_nonunique_raises(self):
10951112
df = pd.DataFrame([[1, 2]], columns=["A", "A"])
10961113
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)