Skip to content

BUG: fix kwargs in Styler.where #40845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ Other
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
- Bug in :class:`Styler` where multiple elements in CSS-selectors were not correctly added to ``table_styles`` (:issue:`39942`)
- Bug in :class:`.Styler` where copying from Jupyter dropped top left cell and misaligned headers (:issue:`12147`)
- Bug in :class:`.Styler.where` where ``kwargs`` were not passed to the applicable callable (:issue:`40845`)
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
Expand Down
6 changes: 4 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,8 @@ def where(
Parameters
----------
cond : callable
``cond`` should take a scalar and return a boolean.
``cond`` should take a scalar, and optional keyword arguments, and return
a boolean.
value : str
Applied when ``cond`` returns true.
other : str
Expand Down Expand Up @@ -1117,7 +1118,8 @@ def where(
other = ""

return self.applymap(
lambda val: value if cond(val) else other, subset=subset, **kwargs
lambda val: value if cond(val, **kwargs) else other,
subset=subset,
)

def set_precision(self, precision: int) -> Styler:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,21 @@ def g(x):
expected = self.df.style.applymap(g, subset=slice_)._compute().ctx
assert result == expected

def test_where_kwargs(self):
df = DataFrame([[1, 2], [3, 4]])

def f(x, val):
return x > val

result = df.style.where(f, "color:green;", "color:red;", val=2)._compute().ctx
expected = {
(0, 0): [("color", "red")],
(0, 1): [("color", "red")],
(1, 0): [("color", "green")],
(1, 1): [("color", "green")],
}
assert result == expected

def test_empty(self):
df = DataFrame({"A": [1, 0]})
s = df.style
Expand Down