-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: multirow
naive implementation Styler.to_latex
part1
#43369
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
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7779b6
multirow naive implementation
attack68 11ac603
multirow naive implementation
attack68 121062f
Merge remote-tracking branch 'upstream/master' into latex_multirow_naive
attack68 20f9b31
fix tests
attack68 f561651
Merge remote-tracking branch 'upstream/master' into latex_multirow_naive
attack68 1732772
fixture
attack68 318bc7b
clean up tests with fixture
attack68 6fa0b35
(ivanomg req) ref tests
attack68 237bee2
ivoanovmg req
attack68 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,14 @@ def df(): | |
return DataFrame({"A": [0, 1], "B": [-0.61, -1.22], "C": ["ab", "cd"]}) | ||
|
||
|
||
@pytest.fixture | ||
def df_ext(df): | ||
df = df.copy() | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
return df | ||
|
||
|
||
@pytest.fixture | ||
def styler(df): | ||
return Styler(df, uuid_len=0, precision=2) | ||
|
@@ -210,11 +218,9 @@ def test_multiindex_columns(df): | |
assert expected == s.to_latex(sparse_columns=False) | ||
|
||
|
||
def test_multiindex_row(df): | ||
def test_multiindex_row(df_ext): | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
df.index = ridx | ||
df_ext.index = ridx | ||
expected = dedent( | ||
"""\ | ||
\\begin{tabular}{llrrl} | ||
|
@@ -225,8 +231,9 @@ def test_multiindex_row(df): | |
\\end{tabular} | ||
""" | ||
) | ||
s = df.style.format(precision=2) | ||
assert expected == s.to_latex() | ||
styler = df_ext.style.format(precision=2) | ||
result = styler.to_latex() | ||
assert expected == result | ||
|
||
# non-sparse | ||
expected = dedent( | ||
|
@@ -239,15 +246,32 @@ def test_multiindex_row(df): | |
\\end{tabular} | ||
""" | ||
) | ||
assert expected == s.to_latex(sparse_index=False) | ||
result = styler.to_latex(sparse_index=False) | ||
assert expected == result | ||
|
||
|
||
def test_multirow_naive(df_ext): | ||
ridx = MultiIndex.from_tuples([("X", "x"), ("X", "x"), ("Y", "y")]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to avoid a duplicate in the tuples (like This would do: [("X", "x"), ("X", "z"), ("Y", "y")] |
||
df_ext.index = ridx | ||
expected = dedent( | ||
"""\ | ||
\\begin{tabular}{llrrl} | ||
& & A & B & C \\\\ | ||
X & x & 0 & -0.61 & ab \\\\ | ||
& x & 1 & -1.22 & cd \\\\ | ||
Y & y & 2 & -2.22 & de \\\\ | ||
\\end{tabular} | ||
""" | ||
) | ||
styler = df_ext.style.format(precision=2) | ||
result = styler.to_latex(multirow_align="naive") | ||
assert expected == result | ||
|
||
|
||
def test_multiindex_row_and_col(df): | ||
def test_multiindex_row_and_col(df_ext): | ||
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")]) | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
df.index, df.columns = ridx, cidx | ||
df_ext.index, df_ext.columns = ridx, cidx | ||
expected = dedent( | ||
"""\ | ||
\\begin{tabular}{llrrl} | ||
|
@@ -259,8 +283,9 @@ def test_multiindex_row_and_col(df): | |
\\end{tabular} | ||
""" | ||
) | ||
s = df.style.format(precision=2) | ||
assert s.to_latex(multirow_align="b", multicol_align="l") == expected | ||
styler = df_ext.style.format(precision=2) | ||
result = styler.to_latex(multirow_align="b", multicol_align="l") | ||
assert result == expected | ||
|
||
# non-sparse | ||
expected = dedent( | ||
|
@@ -274,16 +299,15 @@ def test_multiindex_row_and_col(df): | |
\\end{tabular} | ||
""" | ||
) | ||
assert s.to_latex(sparse_index=False, sparse_columns=False) == expected | ||
result = styler.to_latex(sparse_index=False, sparse_columns=False) | ||
assert result == expected | ||
|
||
|
||
def test_multi_options(df): | ||
def test_multi_options(df_ext): | ||
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")]) | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
df.index, df.columns = ridx, cidx | ||
styler = df.style.format(precision=2) | ||
df_ext.index, df_ext.columns = ridx, cidx | ||
styler = df_ext.style.format(precision=2) | ||
|
||
expected = dedent( | ||
"""\ | ||
|
@@ -292,7 +316,8 @@ def test_multi_options(df): | |
\\multirow[c]{2}{*}{A} & a & 0 & -0.61 & ab \\\\ | ||
""" | ||
) | ||
assert expected in styler.to_latex() | ||
result = styler.to_latex() | ||
assert expected in result | ||
|
||
with option_context("styler.latex.multicol_align", "l"): | ||
assert " & & \\multicolumn{2}{l}{Z} & Y \\\\" in styler.to_latex() | ||
|
@@ -311,30 +336,25 @@ def test_multiindex_columns_hidden(): | |
assert "{tabular}{lrrr}" in s.to_latex() | ||
|
||
|
||
def test_sparse_options(df): | ||
@pytest.mark.parametrize( | ||
"option, value", | ||
[ | ||
("styler.sparse.index", True), | ||
("styler.sparse.index", False), | ||
("styler.sparse.columns", True), | ||
("styler.sparse.columns", False), | ||
], | ||
) | ||
def test_sparse_options(df_ext, option, value): | ||
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")]) | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df.index, df.columns = ridx, cidx | ||
s = df.style | ||
df_ext.index, df_ext.columns = ridx, cidx | ||
styler = df_ext.style | ||
|
||
latex1 = s.to_latex() | ||
|
||
with option_context("styler.sparse.index", True): | ||
latex2 = s.to_latex() | ||
assert latex1 == latex2 | ||
|
||
with option_context("styler.sparse.index", False): | ||
latex2 = s.to_latex() | ||
assert latex1 != latex2 | ||
|
||
with option_context("styler.sparse.columns", True): | ||
latex2 = s.to_latex() | ||
assert latex1 == latex2 | ||
|
||
with option_context("styler.sparse.columns", False): | ||
latex2 = s.to_latex() | ||
assert latex1 != latex2 | ||
latex1 = styler.to_latex() | ||
with option_context(option, value): | ||
latex2 = styler.to_latex() | ||
assert (latex1 == latex2) is value | ||
|
||
|
||
def test_hidden_index(styler): | ||
|
@@ -352,16 +372,14 @@ def test_hidden_index(styler): | |
|
||
|
||
@pytest.mark.parametrize("environment", ["table", "figure*", None]) | ||
def test_comprehensive(df, environment): | ||
def test_comprehensive(df_ext, environment): | ||
# test as many low level features simultaneously as possible | ||
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")]) | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
df.index, df.columns = ridx, cidx | ||
s = df.style | ||
s.set_caption("mycap") | ||
s.set_table_styles( | ||
df_ext.index, df_ext.columns = ridx, cidx | ||
stlr = df_ext.style | ||
stlr.set_caption("mycap") | ||
stlr.set_table_styles( | ||
[ | ||
{"selector": "label", "props": ":{fig§item}"}, | ||
{"selector": "position", "props": ":h!"}, | ||
|
@@ -373,8 +391,8 @@ def test_comprehensive(df, environment): | |
{"selector": "rowcolors", "props": ":{3}{pink}{}"}, # custom command | ||
] | ||
) | ||
s.highlight_max(axis=0, props="textbf:--rwrap;cellcolor:[rgb]{1,1,0.6}--rwrap") | ||
s.highlight_max(axis=None, props="Huge:--wrap;", subset=[("Z", "a"), ("Z", "b")]) | ||
stlr.highlight_max(axis=0, props="textbf:--rwrap;cellcolor:[rgb]{1,1,0.6}--rwrap") | ||
stlr.highlight_max(axis=None, props="Huge:--wrap;", subset=[("Z", "a"), ("Z", "b")]) | ||
|
||
expected = ( | ||
"""\ | ||
|
@@ -398,7 +416,8 @@ def test_comprehensive(df, environment): | |
\\end{table} | ||
""" | ||
).replace("table", environment if environment else "table") | ||
assert s.format(precision=2).to_latex(environment=environment) == expected | ||
result = stlr.format(precision=2).to_latex(environment=environment) | ||
assert result == expected | ||
|
||
|
||
def test_environment_option(styler): | ||
|
@@ -687,13 +706,11 @@ def test_longtable_caption_label(styler, caption, cap_exp, label, lab_exp): | |
(False, False), | ||
], | ||
) | ||
def test_apply_map_header_render_mi(df, index, columns, siunitx): | ||
def test_apply_map_header_render_mi(df_ext, index, columns, siunitx): | ||
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")]) | ||
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")]) | ||
df.loc[2, :] = [2, -2.22, "de"] | ||
df = df.astype({"A": int}) | ||
df.index, df.columns = ridx, cidx | ||
styler = df.style | ||
df_ext.index, df_ext.columns = ridx, cidx | ||
styler = df_ext.style | ||
|
||
func = lambda v: "bfseries: --rwrap" if "A" in v or "Z" in v or "c" in v else None | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about just this?
I mean, there is probably no reason to couple two fixtures together.