Skip to content

Commit d569905

Browse files
sofiane87gfyoung
authored andcommitted
TST: Adding styler applymap multindex & code test [Ref: #25858] (#29346)
* TST: Adding styler applymap multindex & code test Closes gh-25858
1 parent cab3a0e commit d569905

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/io/formats/test_style.py

+19
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ def color_negative_red(val):
376376

377377
(df.style.applymap(color_negative_red, subset=idx[:, idx["b", "d"]]).render())
378378

379+
def test_applymap_subset_multiindex_code(self):
380+
# https://github.com/pandas-dev/pandas/issues/25858
381+
# Checks styler.applymap works with multindex when codes are provided
382+
codes = np.array([[0, 0, 1, 1], [0, 1, 0, 1]])
383+
columns = pd.MultiIndex(
384+
levels=[["a", "b"], ["%", "#"]], codes=codes, names=["", ""]
385+
)
386+
df = DataFrame(
387+
[[1, -1, 1, 1], [-1, 1, 1, 1]], index=["hello", "world"], columns=columns
388+
)
389+
pct_subset = pd.IndexSlice[:, pd.IndexSlice[:, "%":"%"]]
390+
391+
def color_negative_red(val):
392+
color = "red" if val < 0 else "black"
393+
return "color: %s" % color
394+
395+
df.loc[pct_subset]
396+
df.style.applymap(color_negative_red, subset=pct_subset)
397+
379398
def test_where_with_one_style(self):
380399
# GH 17474
381400
def f(x):

0 commit comments

Comments
 (0)