Skip to content
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

to_latex bug for midrule location #18326

Closed
rdturnermtl opened this issue Nov 16, 2017 · 3 comments · Fixed by #20797
Closed

to_latex bug for midrule location #18326

rdturnermtl opened this issue Nov 16, 2017 · 3 comments · Fixed by #20797
Labels
Milestone

Comments

@rdturnermtl
Copy link

There are a lot of other posts (e.g. #14484 #16718 #17959 etc) with bugs in the to_latex() function so this might have a common source.

The midrule location is wrong when the index has a name but index_names=False. This bug shows up even in simple dataframes without multi-indexing like the other bugs open at the moment:

import pandas as pd
import numpy as np
df = pd.DataFrame(data=np.random.randn(5,3))
df.index.name = 'foo'
print df.to_latex(index_names=False)

gives

\begin{tabular}{lrrr}
\toprule
{} &         0 &         1 &         2 \\
0 &  1.898767 & -0.841932 & -1.681799 \\
\midrule
1 &  0.057378 &  0.041809 &  0.612301 \\
2 &  0.799285 &  0.477284 & -0.331426 \\
3 & -0.412104 & -0.680420 & -1.092955 \\
4 & -1.485436 & -1.180868 & -0.063881 \\
\bottomrule
\end{tabular}

It should be

\begin{tabular}{lrrr}
\toprule
{} &         0 &         1 &         2 \\
\midrule
0 &  1.898767 & -0.841932 & -1.681799 \\
1 &  0.057378 &  0.041809 &  0.612301 \\
2 &  0.799285 &  0.477284 & -0.331426 \\
3 & -0.412104 & -0.680420 & -1.092955 \\
4 & -1.485436 & -1.180868 & -0.063881 \\
\bottomrule
\end{tabular}

Also, the width of {} is ignored for the space padding in the index column, which is a bit annoying.

This is from:
Python 2.7.13
numpy==1.13.1
pandas==0.20.3

@gfyoung gfyoung added IO LaTeX to_latex Bug labels Nov 16, 2017
@gfyoung
Copy link
Member

gfyoung commented Nov 16, 2017

@rdturnermtl : Thanks for reporting this! Definitely a bug no doubt. Investigation and PR are welcome!

Looks related to #14484 and #16718

@Michael-E-Rose
Copy link

The bug is still around with pandas 1.0.3:

import pandas as pd
import numpy as np
df = pd.DataFrame(data=np.random.randn(5,3))
df.index.name = 'foo'
print(df.to_latex(index=False))

Must I open a new issue?

@LCarnovale
Copy link

Still seems to be a bug in 1.3.5, I was able to fix it by resetting the index (with drop=True) before calling .to_latex(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants