Skip to content

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

Conversation

attack68
Copy link
Contributor

@attack68 attack68 commented Sep 2, 2021

this is also for compatability with DataFrame.to_latex. #41649

I think its a nice feature though, although opinions on the argument input choice are welcome. Maybe "none" is better than "naive".

When implementing this for multicol there will be the option for left or right alignment, so the likes of "naive-left" or "naive-r" will be used. see #43382

@attack68 attack68 changed the title ENH: multirow naive implementation Styler.to_latex ENH: multirow naive implementation Styler.to_latex part1 Sep 3, 2021
@attack68 attack68 marked this pull request as draft September 4, 2021 10:59
@attack68 attack68 marked this pull request as ready for review September 4, 2021 12:21
@jreback jreback added this to the 1.4 milestone Sep 4, 2021
@jreback jreback added IO LaTeX to_latex Styler conditional formatting using DataFrame.style labels Sep 4, 2021
@jreback
Copy link
Contributor

jreback commented Sep 4, 2021

seems ok, ideally let's have a latex user comment here. umm @bashtage IIRC?

@jreback
Copy link
Contributor

jreback commented Sep 6, 2021

cc @ivanovmg if any comments

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.

Minor comments from my side, regarding the test.

As for the name of the kwarg, then on the one hand I would suggest "none" over "naive".
But on the other hand, considering the similar implementation for the multicolumns, "naive" seems to be fine (I mean, "none-left" and "none-right" would look bizarre, in case of multicols IMHO).

Comment on lines 247 to 248
df.loc[2, :] = [2, -2.22, "de"]
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.

Consider simplifying the dataframe creation to this:

df = DataFrame(
    {"A": [0, 1, 2], "B": [-0.61, -1.22, -2.22], "C": ["ab", "cd", "de"]},
    index=ridx,
)

This way, it is clearer what the dataframe actually look like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

used in multiple tests so I cleaned up module with a fixture

@@ -242,6 +242,25 @@ def test_multiindex_row(df):
assert expected == s.to_latex(sparse_index=False)


def test_multirow_naive(df):
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("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 suggest that the letters A and B should not be repeated in both the index and columns.
What if you use these for the index?

ridx = MultiIndex.from_tuples([("X", "x"), ("X", "y"), ("Z", "z")])

I guess it will be easier to differentiate from the columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fully agree. silly oversight.

"""
)
s = df.style.format(precision=2)
assert expected == s.to_latex(multirow_align="naive")
Copy link
Member

Choose a reason for hiding this comment

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

I suggest that result variable is extracted:

result = s.to_latex(multirow_align="naive")
assert result == expected

Besides, s is not a preferred variable name, because one-letter variables make debugging more complicated.
What about making it more explicit, styler?

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 cleaned up module wide (somewhat)

@attack68 attack68 requested a review from ivanovmg September 6, 2021 17:55
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 like a nice tests clean-up.
A couple of follow-up comments.

df = df.copy()
df.loc[2, :] = [2, -2.22, "de"]
df = df.astype({"A": int})
return df
Copy link
Member

Choose a reason for hiding this comment

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

What about just this?

return DataFrame({"A": [0, 1, 2], "B": [-0.61, -1.22, -2.22], "C": ["ab", "cd", "de"]})

I mean, there is probably no reason to couple two fixtures together.



def test_multirow_naive(df_ext):
ridx = MultiIndex.from_tuples([("X", "x"), ("X", "x"), ("Y", "y")])
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better to avoid a duplicate in the tuples (like ("X", "x")).

This would do:

[("X", "x"), ("X", "z"), ("Y", "y")]

@attack68
Copy link
Contributor Author

attack68 commented Sep 6, 2021

Looks like a nice tests clean-up.
A couple of follow-up comments.

sure. done.

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 73c473d into pandas-dev:master Sep 7, 2021
@attack68 attack68 deleted the latex_multirow_naive branch September 7, 2021 05:26
feefladder pushed a commit to feefladder/pandas that referenced this pull request Sep 7, 2021
…dev#43369)

* multirow naive implementation

* multirow naive implementation

* fix tests

* fixture

* clean up tests with fixture

* (ivanomg req) ref tests

* ivoanovmg req

Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO LaTeX to_latex Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants