Skip to content

Commit e4162cd

Browse files
authored
DEPR: null_color since color used everywhere else (#45907)
1 parent e36db19 commit e4162cd

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

doc/source/user_guide/style.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@
11311131
"source": [
11321132
"df2.iloc[0,2] = np.nan\n",
11331133
"df2.iloc[4,3] = np.nan\n",
1134-
"df2.loc[:4].style.highlight_null(null_color='yellow')"
1134+
"df2.loc[:4].style.highlight_null(color='yellow')"
11351135
]
11361136
},
11371137
{

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Styler
2222
- New method :meth:`.Styler.to_string` for alternative customisable output methods (:issue:`44502`)
2323
- Added the ability to render ``border`` and ``border-{side}`` CSS properties in Excel (:issue:`42276`)
2424
- Added a new method :meth:`.Styler.concat` which allows adding customised footer rows to visualise additional calculations on the data, e.g. totals and counts etc. (:issue:`43875`, :issue:`46186`)
25+
- :meth:`.Styler.highlight_null` now accepts ``color`` consistently with other builtin methods and deprecates ``null_color`` although this remains backwards compatible (:issue:`45907`)
2526

2627
.. _whatsnew_150.enhancements.enhancement2:
2728

pandas/io/formats/style.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from pandas._config import get_option
2121

22+
from pandas._libs import lib
2223
from pandas._typing import (
2324
Axis,
2425
FilePath,
@@ -3203,19 +3204,23 @@ def bar(
32033204

32043205
return self
32053206

3206-
@Substitution(subset=subset, props=props)
3207+
@Substitution(subset=subset, props=props, color=color)
32073208
def highlight_null(
32083209
self,
3209-
null_color: str = "red",
3210+
color: str | None = None,
32103211
subset: Subset | None = None,
32113212
props: str | None = None,
3213+
null_color=lib.no_default,
32123214
) -> Styler:
32133215
"""
32143216
Highlight missing values with a style.
32153217
32163218
Parameters
32173219
----------
3218-
null_color : str, default 'red'
3220+
%(color)s
3221+
3222+
.. versionadded:: 1.5.0
3223+
32193224
%(subset)s
32203225
32213226
.. versionadded:: 1.1.0
@@ -3224,6 +3229,13 @@ def highlight_null(
32243229
32253230
.. versionadded:: 1.3.0
32263231
3232+
null_color : str, default None
3233+
The background color for highlighting.
3234+
3235+
.. deprecated:: 1.5.0
3236+
Use ``color`` instead. If ``color`` is given ``null_color`` is
3237+
not used.
3238+
32273239
Returns
32283240
-------
32293241
self : Styler
@@ -3239,8 +3251,19 @@ def highlight_null(
32393251
def f(data: DataFrame, props: str) -> np.ndarray:
32403252
return np.where(pd.isna(data).to_numpy(), props, "")
32413253

3254+
if null_color != lib.no_default:
3255+
warnings.warn(
3256+
"`null_color` is deprecated: use `color` instead",
3257+
FutureWarning,
3258+
stacklevel=find_stack_level(),
3259+
)
3260+
3261+
if color is None and null_color == lib.no_default:
3262+
color = "red"
3263+
elif color is None and null_color != lib.no_default:
3264+
color = null_color
32423265
if props is None:
3243-
props = f"background-color: {null_color};"
3266+
props = f"background-color: {color};"
32443267
return self.apply(f, axis=None, subset=subset, props=props)
32453268

32463269
@Substitution(subset=subset, color=color, props=props)

pandas/tests/io/formats/style/test_deprecated.py

+5
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,8 @@ def test_precision(df):
163163
def test_render(df):
164164
with tm.assert_produces_warning(FutureWarning):
165165
df.style.render()
166+
167+
168+
def test_null_color(df):
169+
with tm.assert_produces_warning(FutureWarning):
170+
df.style.highlight_null(null_color="blue")

pandas/tests/io/formats/style/test_highlight.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def test_highlight_null(styler):
3737
def test_highlight_null_subset(styler):
3838
# GH 31345
3939
result = (
40-
styler.highlight_null(null_color="red", subset=["A"])
41-
.highlight_null(null_color="green", subset=["B"])
40+
styler.highlight_null(color="red", subset=["A"])
41+
.highlight_null(color="green", subset=["B"])
4242
._compute()
4343
.ctx
4444
)

0 commit comments

Comments
 (0)