Skip to content

BUG: to_latex with multicolumn and multiindex joins cells which are on different hierarchy levels #16719

Closed
@wlter

Description

@wlter

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np


df = pd.DataFrame()


idx = 0
for i in range(2):
  for val0 in [3,2,1]:
    for val1 in range(val0):
      idx = idx + 1
      r0 = str(np.mod(int(idx*0.5),2))
      r1 = np.random.uniform()
      df = df.append({"i":i,"val0" : val0,\
                            "val1":val1, 
                            "r0":r0, \
                          "r1":r1}, ignore_index=True)


df.set_index(["i",'val0','val1',"r0", 'r1'], inplace=True)

print(df.to_latex(multirow = True, escape=False))

produces

\begin{tabular}{lllll}
	\toprule
	&     &     &   &          \\
	i & val0 & val1 & r0 & r1 \\
	\midrule
	\multirow{6}{*}{0.0} & \multirow{3}{*}{3.0} & 0.0 & 0 & 0.307919 \\
	&     & 1.0 & \multirow{2}{*}{1} & 0.488816 \\
	&     & 2.0 &   & 0.708405 \\
	\cline{2-5}
	\cline{4-5}
	& \multirow{2}{*}{2.0} & 0.0 & \multirow{2}{*}{0} & 0.806916 \\
	&     & 1.0 &   & 0.763446 \\
	\cline{2-5}
	\cline{4-5}
	& 1.0 & \multirow{2}{*}{0.0} & \multirow{2}{*}{1} & 0.255642 \\
	\cline{1-5}
	\multirow{6}{*}{1.0} & \multirow{3}{*}{3.0} &     &   & 0.093269 \\
	\cline{3-5}
	\cline{4-5}
	&     & 1.0 & \multirow{2}{*}{0} & 0.775120 \\
	&     & 2.0 &   & 0.989241 \\
	\cline{2-5}
	\cline{4-5}
	& \multirow{2}{*}{2.0} & 0.0 & \multirow{2}{*}{1} & 0.741230 \\
	&     & 1.0 &   & 0.960813 \\
	\cline{2-5}
	\cline{4-5}
	& 1.0 & 0.0 & 0 & 0.559090 \\
	\bottomrule
\end{tabular}


produces

to_latex-bug2

Problem description

Hey,

when using a multi-index dataframe, the to_latex command with option "multirow = True" joins cells, which due to the multirow hierarchy shouldn't be joined. In the image this produces the joined entry on the divider line.

Expected Output

  • cells should not be joint if the hierarchy divides them

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.3.final.0 python-bits: 32 OS: Windows OS-release: 7 machine: AMD64 processor: AMD64 Family 16 Model 4 Stepping 3, AuthenticAMD byteorder: little LC_ALL: None LANG: en LOCALE: None.None

pandas: 0.20.2
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions