From b002086a00abff21a05ed224dd7c76b8dc9c0a1d Mon Sep 17 00:00:00 2001 From: Max Chen Date: Mon, 27 Jan 2020 23:35:18 +0800 Subject: [PATCH 1/4] ENH: `Styler.highlight_null` can accepts `subset` argument --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/io/formats/style.py | 8 ++++++-- pandas/tests/io/formats/test_style.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 920919755dc23..c2685932585a5 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -42,6 +42,7 @@ Other enhancements ^^^^^^^^^^^^^^^^^^ - :class:`Styler` may now render CSS more efficiently where multiple cells have the same styling (:issue:`30876`) +- :meth:`Styler.highlight_null` now accepts ``subset`` argument (:issue:`31345`) - - diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 565752e269d79..39ccd6c25d3b4 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1006,19 +1006,23 @@ def hide_columns(self, subset) -> "Styler": def _highlight_null(v, null_color: str) -> str: return f"background-color: {null_color}" if pd.isna(v) else "" - def highlight_null(self, null_color: str = "red") -> "Styler": + def highlight_null(self, null_color: str = "red", subset=None) -> "Styler": """ Shade the background ``null_color`` for missing values. Parameters ---------- null_color : str + subset : IndexSlice, default None + A valid slice for ``data`` to limit the style application to. + + .. versionadded:: 1.1.0 Returns ------- self : Styler """ - self.applymap(self._highlight_null, null_color=null_color) + self.applymap(self._highlight_null, null_color=null_color, subset=subset) return self def background_gradient( diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index a2659079be7c0..a94667a3f0c34 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1091,6 +1091,23 @@ def test_highlight_null(self, null_color="red"): expected = {(0, 0): [""], (1, 0): ["background-color: red"]} assert result == expected + def test_highlight_null_subset(self): + # GH 31345 + df = pd.DataFrame({"A": [0, np.nan], "B": [0, np.nan]}) + result = ( + df.style.highlight_null(null_color="red", subset=["A"]) + .highlight_null(null_color="green", subset=["B"]) + ._compute() + .ctx + ) + expected = { + (0, 0): [""], + (1, 0): ["background-color: red"], + (0, 1): [""], + (1, 1): ["background-color: green"], + } + assert result == expected + def test_nonunique_raises(self): df = pd.DataFrame([[1, 2]], columns=["A", "A"]) with pytest.raises(ValueError): From e22a65657c1c6f2bfe278d38690dfd9dd157edef Mon Sep 17 00:00:00 2001 From: Max Chen Date: Tue, 28 Jan 2020 23:05:38 +0800 Subject: [PATCH 2/4] mentioned default color in docstring --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 39ccd6c25d3b4..4d8a607cef1ab 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1012,7 +1012,7 @@ def highlight_null(self, null_color: str = "red", subset=None) -> "Styler": Parameters ---------- - null_color : str + null_color : str, default 'red' subset : IndexSlice, default None A valid slice for ``data`` to limit the style application to. From 2f1ea7395a54fb1be8de5baeba1e3790e5439b7c Mon Sep 17 00:00:00 2001 From: Max Chen Date: Sun, 1 Mar 2020 23:06:26 +0800 Subject: [PATCH 3/4] modify docstring --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 4d8a607cef1ab..2b91f5d82abf9 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1013,7 +1013,7 @@ def highlight_null(self, null_color: str = "red", subset=None) -> "Styler": Parameters ---------- null_color : str, default 'red' - subset : IndexSlice, default None + subset : label or list of labels, default None A valid slice for ``data`` to limit the style application to. .. versionadded:: 1.1.0 From 99715cbf9421735cbe7eab53f1990a4c910e7e13 Mon Sep 17 00:00:00 2001 From: Max Chen Date: Thu, 5 Mar 2020 22:57:01 +0800 Subject: [PATCH 4/4] add annotation --- pandas/io/formats/style.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 9225883e66879..9cdb56dc6362a 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1003,7 +1003,11 @@ def hide_columns(self, subset) -> "Styler": def _highlight_null(v, null_color: str) -> str: return f"background-color: {null_color}" if pd.isna(v) else "" - def highlight_null(self, null_color: str = "red", subset=None) -> "Styler": + def highlight_null( + self, + null_color: str = "red", + subset: Optional[Union[Label, Sequence[Label]]] = None, + ) -> "Styler": """ Shade the background ``null_color`` for missing values.