Skip to content

ENH: multicol naive implementation. part2 #43382

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 7 commits into from
Sep 8, 2021

Conversation

attack68
Copy link
Contributor

@attack68 attack68 commented Sep 3, 2021

follows #43369

@attack68 attack68 marked this pull request as ready for review September 5, 2021 08:39
@jreback jreback added the Styler conditional formatting using DataFrame.style label Sep 6, 2021
@jreback jreback added this to the 1.4 milestone Sep 6, 2021
# Conflicts:
#	pandas/io/formats/style.py
#	pandas/tests/io/formats/style/test_to_latex.py
@attack68
Copy link
Contributor Author

attack68 commented Sep 7, 2021

@ivanovmg you fancy reviewing this one too?

@attack68 attack68 requested a review from ivanovmg September 7, 2021 05:33
Copy link
Member

@ivanovmg ivanovmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments below.

)
def test_multicol_naive(df, multicol_align, siunitx, exp):
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")])
df = df.astype({"A": int})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this conversion necessary?
It seems that A column is already of int type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct seemed to be legacy code (from copy/paste)

@@ -1410,6 +1410,14 @@ def _parse_latex_header_span(
if 'colspan="' in attrs:
colspan = attrs[attrs.find('colspan="') + 9 :] # len('colspan="') = 9
colspan = int(colspan[: colspan.find('"')])
if "naive-l" == multicol_align:
out = f"{{{display_val}}}" if wrap else f"{display_val}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this wrap related to siunitx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. the only time wrap is needed is when siunitx is True, but it only applies to columns headers, and not row headers, due to siunitx package establishing display properties for columns

"""
)
s = df.style.format(precision=2)
assert expected == s.to_latex(multicol_align=multicol_align, siunitx=siunitx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider extracting result and then assert result == expected?
Also, it is better to avoid one-letter variables (s in this case) to make debugging easier.

expected = dedent(
f"""\
\\begin{{tabular}}{{l{"SS" if siunitx else "rr"}l}}
{exp} \\\\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this exp actually refer to a header?
Will it be better if you call it header?

ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")])
df = df.astype({"A": int})
df.columns = ridx
level1 = " & a & b & c" if not siunitx else "{} & {a} & {b} & {c}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of having logic inside tests.
I would suggest to move level1 to pytest.mark.parametrize alongside siunitx.
But it may not fit nicely in one line with the other parameters, so may be leave it as it is.

level1 = " & a & b & c" if not siunitx else "{} & {a} & {b} & {c}"
expected = dedent(
f"""\
\\begin{{tabular}}{{l{"SS" if siunitx else "rr"}l}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@attack68
Copy link
Contributor Author

attack68 commented Sep 7, 2021

@ivanovmg changes made.

I disagree with your philosophy on logic in tests. Personally I prefer parametrizations to focus as much as possible on as few parameters, and be an indicator to the purpose of the test.
The purpose of the test is not really to check siunitx affects the column format and level1 header, it just happens to do this so the test has to be structured correctly. This test is small enough so it doesn't really matter either way but that is at least my reasoning.

Copy link
Member

@ivanovmg ivanovmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@attack68 attack68 merged commit 45982d8 into pandas-dev:master Sep 8, 2021
@attack68 attack68 deleted the latex_multicol_naive branch September 8, 2021 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants