File tree 3 files changed +24
-2
lines changed
3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ Other enhancements
42
42
^^^^^^^^^^^^^^^^^^
43
43
44
44
- :class: `Styler ` may now render CSS more efficiently where multiple cells have the same styling (:issue: `30876 `)
45
+ - :meth: `Styler.highlight_null ` now accepts ``subset `` argument (:issue: `31345 `)
45
46
-
46
47
-
47
48
Original file line number Diff line number Diff line change @@ -1006,19 +1006,23 @@ def hide_columns(self, subset) -> "Styler":
1006
1006
def _highlight_null (v , null_color : str ) -> str :
1007
1007
return f"background-color: { null_color } " if pd .isna (v ) else ""
1008
1008
1009
- def highlight_null (self , null_color : str = "red" ) -> "Styler" :
1009
+ def highlight_null (self , null_color : str = "red" , subset = None ) -> "Styler" :
1010
1010
"""
1011
1011
Shade the background ``null_color`` for missing values.
1012
1012
1013
1013
Parameters
1014
1014
----------
1015
1015
null_color : str
1016
+ subset : IndexSlice, default None
1017
+ A valid slice for ``data`` to limit the style application to.
1018
+
1019
+ .. versionadded:: 1.1.0
1016
1020
1017
1021
Returns
1018
1022
-------
1019
1023
self : Styler
1020
1024
"""
1021
- self .applymap (self ._highlight_null , null_color = null_color )
1025
+ self .applymap (self ._highlight_null , null_color = null_color , subset = subset )
1022
1026
return self
1023
1027
1024
1028
def background_gradient (
Original file line number Diff line number Diff line change @@ -1091,6 +1091,23 @@ def test_highlight_null(self, null_color="red"):
1091
1091
expected = {(0 , 0 ): ["" ], (1 , 0 ): ["background-color: red" ]}
1092
1092
assert result == expected
1093
1093
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
+
1094
1111
def test_nonunique_raises (self ):
1095
1112
df = pd .DataFrame ([[1 , 2 ]], columns = ["A" , "A" ])
1096
1113
with pytest .raises (ValueError ):
You can’t perform that action at this time.
0 commit comments