Skip to content

Commit c6dc109

Browse files
committed
TST: Add Styler.where test
1 parent b798cf4 commit c6dc109

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pandas/tests/io/formats/test_style.py

+58
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,64 @@ def f(x):
265265
col in self.df.loc[slice_].columns)
266266
assert result == expected
267267

268+
def test_where_with_one_style(self):
269+
# GH 17474
270+
def f(x):
271+
return x > 0.5
272+
273+
style1 = 'foo: bar'
274+
275+
result = self.df.style.where(f, style1)._compute().ctx
276+
expected = dict(((r, c),
277+
[style1 if f(self.df.loc[row, col]) else ''])
278+
for r, row in enumerate(self.df.index)
279+
for c, col in enumerate(self.df.columns))
280+
assert result == expected
281+
282+
def test_where_subset(self):
283+
# GH 17474
284+
def f(x):
285+
return x > 0.5
286+
287+
style1 = 'foo: bar'
288+
style2 = 'baz: foo'
289+
290+
slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
291+
pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
292+
pd.IndexSlice[:2, ['A', 'B']]]
293+
294+
for slice_ in slices:
295+
result = self.df.style.where(f, style1, style2,
296+
subset=slice_)._compute().ctx
297+
expected = dict(((r, c),
298+
[style1 if f(self.df.loc[row, col]) else style2])
299+
for r, row in enumerate(self.df.index)
300+
for c, col in enumerate(self.df.columns)
301+
if row in self.df.loc[slice_].index and
302+
col in self.df.loc[slice_].columns)
303+
assert result == expected
304+
305+
def test_where_subset_compare_with_applymap(self):
306+
# GH 17474
307+
def f(x):
308+
return x > 0.5
309+
310+
style1 = 'foo: bar'
311+
style2 = 'baz: foo'
312+
313+
def g(x):
314+
return style1 if f(x) else style2
315+
316+
slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
317+
pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
318+
pd.IndexSlice[:2, ['A', 'B']]]
319+
320+
for slice_ in slices:
321+
result = self.df.style.where(f, style1, style2,
322+
subset=slice_)._compute().ctx
323+
expected = self.df.style.applymap(g, subset=slice_)._compute().ctx
324+
assert result == expected
325+
268326
def test_empty(self):
269327
df = pd.DataFrame({'A': [1, 0]})
270328
s = df.style

0 commit comments

Comments
 (0)