question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wltercommented, Jun 19, 2017

So, I wrote my own script, which is in no way compatible - but maybe the idea helps here: I used a hierarchical multi-index for the first levels and left the rest of the hierarchy to default dataframes. In the table, the hierrachichal elements are all distributed over mutliple cells using multirow. The hierarchical elemnts are also creating the clines. I don’t allow multirow in the area of default dara frames. So my suggestion would be to not join cells because of their content but:

on the level of the multiindex:

  • join cells when they belong to the same hierarchical element using multirow
  • add the necessary \clines

on the level of the dataframes (below the multiindex)

  • do not join cells

TLDR: Suggesting to join cells based on whether they are part of multi-index or default dataframe instead of their content

0reactions
mroeschkecommented, Apr 4, 2020

Looks to work on master now. Could use a test

In [7]: In [7]: 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))
\begin{tabular}{lllll}
\toprule
    &     &     &   &          \\
i & val0 & val1 & r0 & r1 \\
\midrule
\multirow{6}{*}{0.0} & \multirow{3}{*}{3.0} & 0.0 & 0 & 0.795703 \\
    &     & 1.0 & 1 & 0.135335 \\
    &     & 2.0 & 1 & 0.210815 \\
\cline{2-5}
    & \multirow{2}{*}{2.0} & 0.0 & 0 & 0.398306 \\
    &     & 1.0 & 0 & 0.753541 \\
\cline{2-5}
    & 1.0 & 0.0 & 1 & 0.753324 \\
\cline{1-5}
\multirow{6}{*}{1.0} & \multirow{3}{*}{3.0} & 0.0 & 1 & 0.606834 \\
    &     & 1.0 & 0 & 0.703867 \\
    &     & 2.0 & 0 & 0.532124 \\
\cline{2-5}
    & \multirow{2}{*}{2.0} & 0.0 & 1 & 0.393957 \\
    &     & 1.0 & 1 & 0.252969 \\
\cline{2-5}
    & 1.0 & 0.0 & 0 & 0.401082 \\
\bottomrule
\end{tabular}


In [8]: pd.__version__
Out[8]: '1.1.0.dev0+1108.gcad602e16'
Read more comments on GitHub >

github_iconTop Results From Across the Web

MultiIndex / advanced indexing — pandas 1.5.2 documentation
The MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects.
Read more >
Pandas dataframe with multiindex column - merge levels
1. Use map and join with string column headers: grouped.columns = grouped.columns. · 2. Use map with format for column headers that have...
Read more >
Hierarchical Indexing | Python Data Science Handbook
MultiIndex for columns¶. In a DataFrame , the rows and columns are completely symmetric, and just as the rows can have multiple levels...
Read more >
Working with MultiIndex in pandas DataFrame
A multi-level index DataFrame is a type of DataFrame that contains multiple level or hierarchical indexing. You can create a MultiIndex ...
Read more >
[Code]-Pandas `to_latex`: how to format multi-index tables?
Here I try to format the columns large and small differently. def f2(x): return f"%.{2}f" % x def f5(x): ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found