Skip to content

Commit 7fdba20

Browse files
attack68JulianWgs
authored andcommitted
BUG: fix kwargs in Styler.where (pandas-dev#40845)
1 parent f4c7103 commit 7fdba20

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ Other
831831
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
832832
- Bug in :class:`Styler` where multiple elements in CSS-selectors were not correctly added to ``table_styles`` (:issue:`39942`)
833833
- Bug in :class:`.Styler` where copying from Jupyter dropped top left cell and misaligned headers (:issue:`12147`)
834+
- Bug in :class:`.Styler.where` where ``kwargs`` were not passed to the applicable callable (:issue:`40845`)
834835
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
835836
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
836837
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)

pandas/io/formats/style.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,8 @@ def where(
10861086
Parameters
10871087
----------
10881088
cond : callable
1089-
``cond`` should take a scalar and return a boolean.
1089+
``cond`` should take a scalar, and optional keyword arguments, and return
1090+
a boolean.
10901091
value : str
10911092
Applied when ``cond`` returns true.
10921093
other : str
@@ -1117,7 +1118,8 @@ def where(
11171118
other = ""
11181119

11191120
return self.applymap(
1120-
lambda val: value if cond(val) else other, subset=subset, **kwargs
1121+
lambda val: value if cond(val, **kwargs) else other,
1122+
subset=subset,
11211123
)
11221124

11231125
def set_precision(self, precision: int) -> Styler:

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

+15
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,21 @@ def g(x):
557557
expected = self.df.style.applymap(g, subset=slice_)._compute().ctx
558558
assert result == expected
559559

560+
def test_where_kwargs(self):
561+
df = DataFrame([[1, 2], [3, 4]])
562+
563+
def f(x, val):
564+
return x > val
565+
566+
result = df.style.where(f, "color:green;", "color:red;", val=2)._compute().ctx
567+
expected = {
568+
(0, 0): [("color", "red")],
569+
(0, 1): [("color", "red")],
570+
(1, 0): [("color", "green")],
571+
(1, 1): [("color", "green")],
572+
}
573+
assert result == expected
574+
560575
def test_empty(self):
561576
df = DataFrame({"A": [1, 0]})
562577
s = df.style

0 commit comments

Comments
 (0)