File tree 3 files changed +29
-3
lines changed
3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,7 @@ Other enhancements
64
64
^^^^^^^^^^^^^^^^^^
65
65
66
66
- :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 `)
67
68
- When writing directly to a sqlite connection :func: `to_sql ` now supports the ``multi `` method (:issue: `29921 `)
68
69
- `OptionError ` is now exposed in `pandas.errors ` (:issue: `27553 `)
69
70
- :func: `timedelta_range ` will now infer a frequency when passed ``start ``, ``stop ``, and ``periods `` (:issue: `32377 `)
Original file line number Diff line number Diff line change @@ -1003,19 +1003,27 @@ def hide_columns(self, subset) -> "Styler":
1003
1003
def _highlight_null (v , null_color : str ) -> str :
1004
1004
return f"background-color: { null_color } " if pd .isna (v ) else ""
1005
1005
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" :
1007
1011
"""
1008
1012
Shade the background ``null_color`` for missing values.
1009
1013
1010
1014
Parameters
1011
1015
----------
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
1013
1021
1014
1022
Returns
1015
1023
-------
1016
1024
self : Styler
1017
1025
"""
1018
- self .applymap (self ._highlight_null , null_color = null_color )
1026
+ self .applymap (self ._highlight_null , null_color = null_color , subset = subset )
1019
1027
return self
1020
1028
1021
1029
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