Skip to content

Commit 8e21df2

Browse files
authored
DEPR: Styler.where possible deprecation (#40821)
1 parent 0fba740 commit 8e21df2

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

pandas/io/formats/style.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ def apply(
611611
612612
See Also
613613
--------
614-
Styler.where: Apply CSS-styles based on a conditional function elementwise.
615614
Styler.applymap: Apply a CSS-styling function elementwise.
616615
617616
Notes
@@ -669,7 +668,6 @@ def applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
669668
670669
See Also
671670
--------
672-
Styler.where: Apply CSS-styles based on a conditional function elementwise.
673671
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.
674672
675673
Notes
@@ -701,6 +699,8 @@ def where(
701699
"""
702700
Apply CSS-styles based on a conditional function elementwise.
703701
702+
.. deprecated:: 1.3.0
703+
704704
Updates the HTML representation with a style which is
705705
selected in accordance with the return value of a function.
706706
@@ -728,13 +728,31 @@ def where(
728728
Styler.applymap: Apply a CSS-styling function elementwise.
729729
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.
730730
731-
Examples
732-
--------
733-
>>> def cond(v):
734-
... return v > 1 and v != 4
731+
Notes
732+
-----
733+
This method is deprecated.
734+
735+
This method is a convenience wrapper for :meth:`Styler.applymap`, which we
736+
recommend using instead.
737+
738+
The example:
735739
>>> df = pd.DataFrame([[1, 2], [3, 4]])
736-
>>> df.style.where(cond, value='color:red;', other='font-size:2em;')
740+
>>> def cond(v, limit=4):
741+
... return v > 1 and v != limit
742+
>>> df.style.where(cond, value='color:green;', other='color:red;')
743+
744+
should be refactored to:
745+
>>> def style_func(v, value, other, limit=4):
746+
... cond = v > 1 and v != limit
747+
... return value if cond else other
748+
>>> df.style.applymap(style_func, value='color:green;', other='color:red;')
737749
"""
750+
warnings.warn(
751+
"this method is deprecated in favour of `Styler.applymap()`",
752+
FutureWarning,
753+
stacklevel=2,
754+
)
755+
738756
if other is None:
739757
other = ""
740758

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ def f(x):
541541

542542
style1 = "foo: bar"
543543

544-
result = self.df.style.where(f, style1)._compute().ctx
544+
with tm.assert_produces_warning(FutureWarning):
545+
result = self.df.style.where(f, style1)._compute().ctx
545546
expected = {
546547
(r, c): [("foo", "bar")]
547548
for r, row in enumerate(self.df.index)
@@ -568,14 +569,15 @@ def f(x):
568569
style1 = "foo: bar"
569570
style2 = "baz: foo"
570571

571-
result = self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
572+
with tm.assert_produces_warning(FutureWarning):
573+
res = self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
572574
expected = {
573575
(r, c): [("foo", "bar") if f(self.df.loc[row, col]) else ("baz", "foo")]
574576
for r, row in enumerate(self.df.index)
575577
for c, col in enumerate(self.df.columns)
576578
if row in self.df.loc[slice_].index and col in self.df.loc[slice_].columns
577579
}
578-
assert result == expected
580+
assert res == expected
579581

580582
def test_where_subset_compare_with_applymap(self):
581583
# GH 17474
@@ -597,9 +599,10 @@ def g(x):
597599
]
598600

599601
for slice_ in slices:
600-
result = (
601-
self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
602-
)
602+
with tm.assert_produces_warning(FutureWarning):
603+
result = (
604+
self.df.style.where(f, style1, style2, subset=slice_)._compute().ctx
605+
)
603606
expected = self.df.style.applymap(g, subset=slice_)._compute().ctx
604607
assert result == expected
605608

@@ -609,14 +612,15 @@ def test_where_kwargs(self):
609612
def f(x, val):
610613
return x > val
611614

612-
result = df.style.where(f, "color:green;", "color:red;", val=2)._compute().ctx
615+
with tm.assert_produces_warning(FutureWarning):
616+
res = df.style.where(f, "color:green;", "color:red;", val=2)._compute().ctx
613617
expected = {
614618
(0, 0): [("color", "red")],
615619
(0, 1): [("color", "red")],
616620
(1, 0): [("color", "green")],
617621
(1, 1): [("color", "green")],
618622
}
619-
assert result == expected
623+
assert res == expected
620624

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

0 commit comments

Comments
 (0)