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
32 changes: 25 additions & 7 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,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 @@ -630,7 +629,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 @@ -662,6 +660,8 @@ def where(
"""
Apply CSS-styles based on a conditional function elementwise.

.. deprecated:: 1.3.0

Updates the HTML representation with a style which is
selected in accordance with the return value of a function.

Expand Down Expand Up @@ -689,13 +689,31 @@ 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 = ""

Expand Down
20 changes: 12 additions & 8 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,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 @@ -568,14 +569,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 @@ -597,9 +599,10 @@ 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

Expand All @@ -609,14 +612,15 @@ def test_where_kwargs(self):
def f(x, val):
return x > val

result = df.style.where(f, "color:green;", "color:red;", val=2)._compute().ctx
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 result == expected
assert res == expected

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