Skip to content
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

DEPR: Styler.where possible deprecation #40821

Merged
merged 10 commits into from
Apr 26, 2021
36 changes: 27 additions & 9 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ def apply(

See Also
--------
Styler.where: Apply CSS-styles based on a conditional function elementwise.
Styler.applymap: Apply a CSS-styling function elementwise.

Notes
Expand Down Expand Up @@ -1045,7 +1044,6 @@ def applymap(self, func: Callable, subset=None, **kwargs) -> Styler:

See Also
--------
Styler.where: Apply CSS-styles based on a conditional function elementwise.
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.

Notes
Expand Down Expand Up @@ -1080,10 +1078,12 @@ def where(
Updates the HTML representation with a style which is
selected in accordance with the return value of a function.

.. deprecated:: 1.3.0

Parameters
----------
cond : callable
``cond`` should take a scalar and return a boolean.
``cond`` should take a scalar, and optional args, and return a boolean.
attack68 marked this conversation as resolved.
Show resolved Hide resolved
value : str
Applied when ``cond`` returns true.
other : str
Expand All @@ -1103,18 +1103,36 @@ def where(
Styler.applymap: Apply a CSS-styling function elementwise.
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.

Examples
--------
>>> def cond(v):
... return v > 1 and v != 4
Notes
-----
This method is deprecated.

This method is a convenience wrapper for :meth:`Styler.applymap`, which we
recommend using instead.

The example:
>>> df = pd.DataFrame([[1, 2], [3, 4]])
>>> df.style.where(cond, value='color:red;', other='font-size:2em;')
>>> def cond(v, limit=4):
... return v > 1 and v != limit
>>> df.style.where(cond, value='color:green;', other='color:red;')

should be refactored to:
>>> def style_func(v, value, other, limit=4):
... cond = v > 1 and v != limit
... return value if cond else other
>>> df.style.applymap(style_func, value='color:green;', other='color:red;')
"""
warnings.warn(
"this method is deprecated in favour of `Styler.applymap()`",
FutureWarning,
stacklevel=2,
)

if other is None:
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
31 changes: 25 additions & 6 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ def f(x):

style1 = "foo: bar"

result = self.df.style.where(f, style1)._compute().ctx
with tm.assert_produces_warning(FutureWarning):
result = self.df.style.where(f, style1)._compute().ctx
expected = {
(r, c): [("foo", "bar")]
for r, row in enumerate(self.df.index)
Expand All @@ -522,14 +523,15 @@ def f(x):
style1 = "foo: bar"
style2 = "baz: foo"

result = self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
with tm.assert_produces_warning(FutureWarning):
res = self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
expected = {
(r, c): [("foo", "bar") if f(self.df.loc[row, col]) else ("baz", "foo")]
for r, row in enumerate(self.df.index)
for c, col in enumerate(self.df.columns)
if row in self.df.loc[slice_].index and col in self.df.loc[slice_].columns
}
assert result == expected
assert res == expected

def test_where_subset_compare_with_applymap(self):
# GH 17474
Expand All @@ -551,12 +553,29 @@ def g(x):
]

for slice_ in slices:
result = (
self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
)
with tm.assert_produces_warning(FutureWarning):
result = (
self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
)
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

with tm.assert_produces_warning(FutureWarning):
res = 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 res == expected

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