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 adds extra empty header row when index has a name even if index=False #26111

Closed
javidcf opened this issue Apr 16, 2019 · 1 comment · Fixed by #40422
Closed

to_latex adds extra empty header row when index has a name even if index=False #26111

javidcf opened this issue Apr 16, 2019 · 1 comment · Fixed by #40422
Labels
Milestone

Comments

@javidcf
Copy link

javidcf commented Apr 16, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'], index=pd.RangeIndex(2, name='Idx'))

# When the index has a name you get an extra header row for it
print(df.to_latex())
# \begin{tabular}{lrr}
# \toprule
# {} &  a &  b \\
# Idx &    &    \\
# \midrule
# 0   &  1 &  2 \\
# 1   &  3 &  4 \\
# \bottomrule
# \end{tabular}

# However, when you do not want the index you still get the extra header row
print(df.to_latex(index=False))
# \begin{tabular}{rr}
# \toprule
#  a &  b \\
#    &    \\
# \midrule
#  1 &  2 \\
#  3 &  4 \\
# \bottomrule
# \end{tabular}

# What I would expect is the same as if there was no index name
print(df.reset_index(drop=True).to_latex(index=False))
# \begin{tabular}{rr}
# \toprule
#  a &  b \\
# \midrule
#  1 &  2 \\
#  3 &  4 \\
# \bottomrule
# \end{tabular}

Problem description

As shown above, producing LaTeX tables with indices that have a name produces an extra row in the header even when index=False. As I see it, this is not correct, since you just get an empty extra header line, and the behavior should be the same as if the data frame did not have an index with a name.

Expected Output

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'], index=pd.RangeIndex(2, name='Idx'))
print(df.to_latex(index=False))
# \begin{tabular}{rr}
# \toprule
#  a &  b \\
# \midrule
#  1 &  2 \\
#  3 &  4 \\
# \bottomrule
# \end{tabular}

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.2
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@javidcf
Copy link
Author

javidcf commented Apr 17, 2019

I don't have the tooling here for a proper PR (plus test?), but I think all this should take is changing this line in pandas/io/formats/format.py:

self.show_index_names = index_names

To something like:

self.show_index_names = index_names and index

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.

4 participants