Skip to content

Commit f05f3b9

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 f05f3b9

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-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 ``LatexFormatter.write_result`` 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

+13-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

@@ -887,6 +886,7 @@ def get_col_type(dtype):
887886
name = any(self.frame.index.names)
888887
cname = any(self.frame.columns.names)
889888
lastcol = self.frame.index.nlevels - 1
889+
previous_lev3 = None
890890
for i, lev in enumerate(self.frame.index.levels):
891891
lev2 = lev.format()
892892
blank = ' ' * len(lev2[0])
@@ -897,11 +897,19 @@ def get_col_type(dtype):
897897
lev3 = [blank] * clevels
898898
if name:
899899
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))
900+
current_idx_val = None
901+
for level_idx in self.frame.index.labels[i]:
902+
if ((previous_lev3 is None or
903+
previous_lev3[len(lev3)].isspace()) and
904+
lev2[level_idx] == current_idx_val):
905+
# same index as above row and left index was the same
906+
lev3.append(blank)
907+
else:
908+
# different value than above or left index different
909+
lev3.append(lev2[level_idx])
910+
current_idx_val = lev2[level_idx]
904911
strcols.insert(i, lev3)
912+
previous_lev3 = lev3
905913

906914
column_format = self.column_format
907915
if column_format is None:

pandas/tests/io/formats/test_to_latex.py

+16
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ 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+
df = pd.DataFrame(index=pd.MultiIndex.from_tuples(
226+
[('A', 'c'), ('B', 'c')]), columns=['col'])
227+
result = df.to_latex()
228+
expected = r"""\begin{tabular}{lll}
229+
\toprule
230+
& & col \\
231+
\midrule
232+
A & c & NaN \\
233+
B & c & NaN \\
234+
\bottomrule
235+
\end{tabular}
220236
"""
221237

222238
assert result == expected

0 commit comments

Comments
 (0)