Skip to content

Conversation

@agartland
Copy link
Contributor

@agartland agartland commented Feb 23, 2017

Used MultiIndex.formatter() to correctly sparsify the index for to_latex().

I was having an issue with printing Latex tables. This can be seen in the comparison of to_string() and to_latex() for the following table. Notice that the first row of Vol8 should not be missing any labels because it marks a new label in the outermost level. This is correct in to_string and incorrect in to_latex.

Output of to_string():

Volunteer Visit Stim Marker Vol6 2 Long CD137+ 96 IFNg+ 192 Short CD154+ 96 14 Long IFNg+ 288 Short CD137+ 192 Vol7 2 Long CD69- 96 IFNg+ 96 Short CD137+ 96 4 Short CD137+ 192 IFNg+ 96 14 Short CD137+ 96 CD154+ 96 IFNg+ 96 Vol8 2 Short CD137+ 288 4 Long IFNg+ 192 

Output of to_latex():

\begin{tabular}{llllr} \toprule & & & & Total cells \\ Volunteer & Visit & Stim & Marker & \\ \midrule Vol6 & 2 & Long & CD137+ & 96 \\ & & & IFNg+ & 192 \\ & & Short & CD154+ & 96 \\ & 14 & Long & IFNg+ & 288 \\ & & Short & CD137+ & 192 \\ Vol7 & 2 & Long & CD69- & 96 \\ & & & IFNg+ & 96 \\ & & Short & CD137+ & 96 \\ & 4 & & & 192 \\ & & & IFNg+ & 96 \\ & 14 & & CD137+ & 96 \\ & & & CD154+ & 96 \\ & & & IFNg+ & 96 \\ Vol8 & 2 & & CD137+ & 288 \\ & 4 & Long & IFNg+ & 192 \\ \bottomrule \end{tabular} 

I've tested the relevant pieces of this code using this example, but I would be grateful for help with adding tests to the repository. I'm not set up to develop the pandas codebase currently and am not exactly sure how to add the right tests in the right places. Thanks!

Used the index formatter to correctly sparsify the MultiIndex for `to_latex()`
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Feb 23, 2017

Tests will go in pandas/tests/formats/test_formats.py, you'll see some other latex tests there.

Could you also post screen shots of the output before and after your fix? A release note too in doc/source/whatsnw/v0.20.0.txt

@jreback
Copy link
Contributor

jreback commented Feb 23, 2017

this is going to need tests

@jreback jreback added IO LaTeX to_latex Output-Formatting __repr__ of pandas objects, to_string labels Feb 23, 2017
@jreback
Copy link
Contributor

jreback commented Mar 28, 2017

can you rebase / update

@jreback
Copy link
Contributor

jreback commented Mar 28, 2017

can you add the test from the issue? and a whatsnew note (bug fixes is fine).

tests are now in pandas/tests/formats/test_to_latex.py

@agartland
Copy link
Contributor Author

I merged from upstream/master where upstream is pandas-dev/pandas. Is that what you meant? I apologize that I have done nothing with this recently. I'll try to get to it soon.

@agartland
Copy link
Contributor Author

Yes, I should be able to generate the test pretty easily. I'll let you know if I need more help.

@jreback
Copy link
Contributor

jreback commented Mar 28, 2017

@agartland yep, that's what I meant.

@jreback
Copy link
Contributor

jreback commented May 7, 2017

can you rebase / update

@TomAugspurger TomAugspurger added this to the 0.20.2 milestone May 8, 2017
@TomAugspurger
Copy link
Contributor

@agartland we're releasing 0.20.2 in a few days, if you want to rebase and update.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

needs a test & whatsnew note.

@agartland
Copy link
Contributor Author

OK I've added a test and a whatsnew note. I will wait to see if the new test passes the CI.
Let me know if I need to do anything else. Sorry for the delay.

Before patch:

image

After patch:

image

@agartland
Copy link
Contributor Author

Looks like m patch broke these two tests:

TestToLatex.test_to_latex_multicolumnrow 

They both fail because they expect a black value to look like:
5 & & 4
but instead it is:
5 & {} & 4

I'm not sure if there's a difference in Latex for these two. The {} comes from code that I didn't write, but now they are inserted in different places. Should I update the tests with {} so they pass?

@TomAugspurger
Copy link
Contributor

@agartland the outputs do look the same to be. Could you do a bit of digging to see who's inserting the {} though? At a glance, it seems to be from LateFormatter.write_result. If that's right, then we should just update the tests.

I think https://travis-ci.org/pandas-dev/pandas/jobs/238131999#L1144 is a real failure though? Here it is, using pandoc, assuming I copy-pasted correctly:

--- header-includes: - \usepackage{booktabs} - \usepackage{multirow} --- # Old \begin{tabular}{ll} \toprule {} & x \\ {} & y \\ \midrule 0 & a \\ \bottomrule \end{tabular} # New \begin{tabular}{lll} \toprule & & 0 \\ \midrule x & y & a \\ \bottomrule \end{tabular} 

screen shot 2017-06-01 at 7 41 15 am

@jreback jreback modified the milestones: 0.21.0, 0.20.2 Jun 2, 2017
@jreback jreback removed this from the 0.21.0 milestone Aug 17, 2017
@jreback
Copy link
Contributor

jreback commented Aug 17, 2017

closing as stale. if you want to update. pls comment.

@jreback jreback closed this Aug 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

IO LaTeX to_latex Output-Formatting __repr__ of pandas objects, to_string

3 participants