Skip to content

Commit e21d2f3

Browse files
author
Maximilian Köstler
committed
BUG: LatexFormatter.write_result multi-index
Fixed GH issue 14484: `LatexFormatter.write_result`` now does not print blanks if a higher-order index differs from the previous row. Also added testcase for this.
1 parent e682902 commit e21d2f3

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ I/O
415415
- Bug in :func:`read_stata` where value labels could not be read when using an iterator (:issue:`16923`)
416416
- Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`)
417417
- Bug in :func:`read_csv` where automatic delimiter detection caused a ``TypeError`` to be thrown when a bad line was encountered rather than the correct error message (:issue:`13374`)
418+
- Bug in :func:`to_latex` where repeated multi-index values were not printed even though a higher level index differed from the previous row (:issue:`14484`)
418419

419420
Plotting
420421
^^^^^^^^

pandas/io/formats/format.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import pandas as pd
4646
import numpy as np
4747

48-
import itertools
4948
import csv
5049
from functools import partial
5150

@@ -897,10 +896,11 @@ def get_col_type(dtype):
897896
lev3 = [blank] * clevels
898897
if name:
899898
lev3.append(lev.name)
900-
for level_idx, group in itertools.groupby(
901-
self.frame.index.labels[i]):
902-
count = len(list(group))
903-
lev3.extend([lev2[level_idx]] + [blank] * (count - 1))
899+
for level_idx, group in self.frame.groupby(
900+
level=list(range(0, i + 1))):
901+
level_idx_val = str(level_idx[-1] if i > 0 else level_idx)
902+
count = group.shape[0]
903+
lev3.extend([level_idx_val] + [blank] * (count - 1))
904904
strcols.insert(i, lev3)
905905

906906
column_format = self.column_format

pandas/tests/io/formats/test_to_latex.py

+22
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ def test_to_latex_multiindex(self):
217217
1 & 2.0 & 3.5 & 0.707107 & 3.0 & 3.25 & 3.5 & 3.75 & 4.0 \\
218218
\bottomrule
219219
\end{tabular}
220+
"""
221+
222+
assert result == expected
223+
224+
# GH 14484
225+
# If an index is repeated in subsequent rows, it should be
226+
# replaced with a blank in the created table.
227+
# This should ONLY happen if all higher order indicies
228+
# (to the left) are equal, too.
229+
# In this test, 'c' has to be printed both times, because
230+
# the higher order index 'A' != 'B'
231+
df = pd.DataFrame(index=pd.MultiIndex.from_tuples(
232+
[('A', 'c'), ('B', 'c')]), columns=['col'])
233+
result = df.to_latex()
234+
expected = r"""\begin{tabular}{lll}
235+
\toprule
236+
& & col \\
237+
\midrule
238+
A & c & NaN \\
239+
B & c & NaN \\
240+
\bottomrule
241+
\end{tabular}
220242
"""
221243

222244
assert result == expected

0 commit comments

Comments
 (0)