10000 ENH: `Styler.apply_index` and `Styler.applymap_index` for conditional formatting of column/index headers by attack68 · Pull Request #41893 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Styler.apply_index and Styler.applymap_index for conditional formatting of column/index headers #41893

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

Merged
merged 31 commits into from
Aug 11, 2021
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ece8855
add apply across index
attack68 Jun 9, 2021
a3a88e5
add applymap across index
attack68 Jun 9, 2021
066e4f3
improve docs
attack68 Jun 9, 2021
50dbcc7
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jun 11, 2021
ef5839f
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jun 12, 2021
5e4c1c0
add column header styling and amend tests
attack68 Jun 12, 2021
20ac7e0
doc sharing
attack68 Jun 13, 2021
26c5340
doc fix
attack68 Jun 13, 2021
2437b72
doc fix
attack68 Jun 13, 2021
312a6e6
collapse the cellstyle maps
attack68 Jun 13, 2021
553426f
add basic test
attack68 Jun 13, 2021
f01dfee
add basic test
attack68 Jun 13, 2021
f940165
parametrise tests
attack68 Jun 13, 2021
6f5b46c
test for raises ValueError
attack68 Jun 13, 2021
75cf6ca
test html working
attack68 Jun 13, 2021
d654139
test html working
attack68 Jun 13, 2021
728091a
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jun 14, 2021
17787ef
whats new 1.4.0
attack68 Jun 14, 2021
248de0d
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jun 20, 2021
321458e
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jun 29, 2021
ed58798
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jul 13, 2021
8d8e88f
update tests
attack68 Jul 14, 2021
e17d766
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Jul 30, 2021
f70bf65
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Aug 6, 2021
3dfe6d5
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Aug 9, 2021
81f3a3c
rename: applymap_header -->> applymap_index
attack68 Aug 9, 2021
e709777
skip doctests
attack68 Aug 9, 2021
1e6efea
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Aug 9, 2021
ab237b0
rename levels -->> level
attack68 Aug 9, 2021
9b7ec06
update user guide
attack68 Aug 9, 2021
1c7ef8b
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 Aug 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
parametrise tests
  • Loading branch information
attack68 committed Jun 13, 2021
commit f9401659a49cc40b3be8b6dcad03e88f676f864f
31 changes: 12 additions & 19 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,26 @@ def test_render_trimming_mi():
assert {"attributes": 'colspan="2"'}.items() <= ctx["head"][0][2].items()


def test_apply_map_header():
@pytest.mark.parametrize("method", ["applymap", "apply"])
@pytest.mark.parametrize("axis", ["index", "columns"])
def test_apply_map_header(method, axis):
df = DataFrame({"A": [0, 0], "B": [1, 1]}, index=["C", "D"])
func = lambda s: ["attr: val" if ("A" in v or "D" in v) else "" for v in s]
func_map = lambda v: "attr: val" if ("A" in v or "D" in v) else ""
func = {
"apply": lambda s: ["attr: val" if ("A" in v or "C" in v) else "" for v in s],
"applymap": lambda v: "attr: val" if ("A" in v or "C" in v) else "",
}

# test execution added to todo
result = df.style.apply_header(func, axis="index")
result = getattr(df.style, f"{method}_header")(func[method], axis=axis)
assert len(result._todo) == 1
assert len(result.ctx_index) == 0
assert len(getattr(result, f"ctx_{axis}")) == 0

# test over index
# test ctx object on compute
result._compute()
result_map = df.style.applymap_header(func_map, axis="index")._compute()
expected = {
(1, 0): [("attr", "val")],
}
assert result.ctx_index == expected
assert result_map.ctx_index == expected

# test over columns
result = df.style.apply_header(func, axis="columns")._compute()
result_map = df.style.applymap_header(func_map, axis="columns")._compute()
expected = {
(0, 0): [("attr", "val")],
}
assert result.ctx_columns == expected
assert result_map.ctx_columns == expected
assert getattr(result, f"ctx_{axis}") == expected


@pytest.mark.parametrize("method", ["apply", "applymap"])
Expand All @@ -192,7 +185,7 @@ def test_apply_map_header_mi(mi_styler, method, axis):
"apply": lambda s: ["attr: val;" if "b" in v else "" for v in s],
"applymap": lambda v: "attr: val" if "b" in v else "",
}
result = getattr(mi_styler, method + "_header")(func[method], axis=axis)._compute()
result = getattr(mi_styler, f"{method}_header")(func[method], axis=axis)._compute()
expected = {(1, 1): [("attr", "val")]}
assert getattr(result, f"ctx_{axis}") == expected

Expand Down
0