BUG: to_latex outputs string with missing second index level values · Issue #14484 · pandas-dev/pandas (original) (raw)

I am using pandas to generate a LaTeX string using the to_latex() method on a DataFrame, which is indexed using a MultiIndex object. Running the code snippet produces an incorrect list of strings: the LaTeX table is missing two index numbers.

Code Snippet

import pandas as pd

outliers_lst = [(23240, 0), (23240, 15), (23240, 23), (23240, 31), (23240, 85), (38661, 85), (41231, 85), (41231, 92), (46371, 0)]

headers = (['max', 'EC 1', 'S'], ['max', 'EC 1', 'A'], ['max', 'EC 2', 'S'])

table = pd.DataFrame("",index = pd.MultiIndex.from_tuples(sorted(outliers_lst)), columns = pd.MultiIndex.from_tuples(headers)) table.to_latex(index = True, longtable = True, column_format = 'c'*5).split('\n')

Incorrect Output

[u'\\begin{longtable}{cccccccccccccccccccccccccc}',
 u'\\toprule',
 u'      &    &  max &   &      \\\\',
 u'      &    & EC 1 &   & EC 2 \\\\',
 u'      &    &    S & A &    S \\\\',
 u'\\midrule',
 u'\\endhead',
 u'\\midrule',
 u'\\multicolumn{3}{r}{{Continued on next page}} \\\\',
 u'\\midrule',
 u'\\endfoot',
 u'',
 u'\\bottomrule',
 u'\\endlastfoot',
 u'23240 & 0  &      &   &      \\\\',
 u'      & 15 &      &   &      \\\\',
 u'      & 23 &      &   &      \\\\',
 u'      & 31 &      &   &      \\\\',
 u'      & 85 &      &   &      \\\\',
 u'38661 &    &      &   &      \\\\',
 u'41231 &    &      &   &      \\\\',
 u'      & 92 &      &   &      \\\\',
 u'46371 & 0  &      &   &      \\\\',
 u'\\end{longtable}',
 u'']

Correct Output

[u'\\begin{longtable}{cccccccccccccccccccccccccc}',
 u'\\toprule',
 u'      &    &  max &   &      \\\\',
 u'      &    & EC 1 &   & EC 2 \\\\',
 u'      &    &    S & A &    S \\\\',
 u'\\midrule',
 u'\\endhead',
 u'\\midrule',
 u'\\multicolumn{3}{r}{{Continued on next page}} \\\\',
 u'\\midrule',
 u'\\endfoot',
 u'',
 u'\\bottomrule',
 u'\\endlastfoot',
 u'23240 & 0  &      &   &      \\\\',
 u'      & 15 &      &   &      \\\\',
 u'      & 23 &      &   &      \\\\',
 u'      & 31 &      &   &      \\\\',
 u'      & 85 &      &   &      \\\\',
 u'38661 & 85 &      &   &      \\\\',
 u'41231 & 85 &      &   &      \\\\',
 u'      & 92 &      &   &      \\\\',
 u'46371 & 0  &      &   &      \\\\',
 u'\\end{longtable}',
 u'']

Note that in the correct output LaTeX strings, the rows with indices (38661, 85) and (41231, 85) correctly include the second index (the number 85), but in the incorrect LaTeX strings the rows do not include the number 85.

Could this be because the row (23240, 85) above (38661, 85) includes 85 in its second index?

commit: None python: 2.7.12.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.1
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: 0.7.6.None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None