Skip to content

Commit 0ebdc10

Browse files
gfyoungjreback
authored andcommitted
BUG: LatexFormatter.write_result multi-index (#18685)
* 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. * MAINT: Address reviewer comments Closes gh-14484 Closes gh-17499
1 parent 1fa55d2 commit 0ebdc10

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ I/O
120120
- Bug in :meth:`DataFrame.to_msgpack` when serializing data of the numpy.bool_ datatype (:issue:`18390`)
121121
- Bug in :func:`read_json` not decoding when reading line deliminted JSON from S3 (:issue:`17200`)
122122
- Bug in :func:`pandas.io.json.json_normalize` to avoid modification of ``meta`` (:issue:`18610`)
123+
- 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`)
123124

124125
Plotting
125126
^^^^^^^^

pandas/io/formats/format.py

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

49-
import itertools
5049
import csv
5150
from functools import partial
5251

@@ -903,6 +902,7 @@ def get_col_type(dtype):
903902
name = any(self.frame.index.names)
904903
cname = any(self.frame.columns.names)
905904
lastcol = self.frame.index.nlevels - 1
905+
previous_lev3 = None
906906
for i, lev in enumerate(self.frame.index.levels):
907907
lev2 = lev.format()
908908
blank = ' ' * len(lev2[0])
@@ -913,11 +913,19 @@ def get_col_type(dtype):
913913
lev3 = [blank] * clevels
914914
if name:
915915
lev3.append(lev.name)
916-
for level_idx, group in itertools.groupby(
917-
self.frame.index.labels[i]):
918-
count = len(list(group))
919-
lev3.extend([lev2[level_idx]] + [blank] * (count - 1))
916+
current_idx_val = None
917+
for level_idx in self.frame.index.labels[i]:
918+
if ((previous_lev3 is None or
919+
previous_lev3[len(lev3)].isspace()) and
920+
lev2[level_idx] == current_idx_val):
921+
# same index as above row and left index was the same
922+
lev3.append(blank)
923+
else:
924+
# different value than above or left index different
925+
lev3.append(lev2[level_idx])
926+
current_idx_val = lev2[level_idx]
920927
strcols.insert(i, lev3)
928+
previous_lev3 = lev3
921929

922930
column_format = self.column_format
923931
if column_format is None:

pandas/tests/io/formats/test_to_latex.py

+22
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ def test_to_latex_multiindex(self):
221221

222222
assert result == expected
223223

224+
def test_to_latex_multiindex_dupe_level(self):
225+
# see gh-14484
226+
#
227+
# If an index is repeated in subsequent rows, it should be
228+
# replaced with a blank in the created table. This should
229+
# ONLY happen if all higher order indices (to the left) are
230+
# equal too. In this test, 'c' has to be printed both times
231+
# because the higher order index 'A' != 'B'.
232+
df = pd.DataFrame(index=pd.MultiIndex.from_tuples(
233+
[('A', 'c'), ('B', 'c')]), columns=['col'])
234+
result = df.to_latex()
235+
expected = r"""\begin{tabular}{lll}
236+
\toprule
237+
& & col \\
238+
\midrule
239+
A & c & NaN \\
240+
B & c & NaN \\
241+
\bottomrule
242+
\end{tabular}
243+
"""
244+
assert result == expected
245+
224246
def test_to_latex_multicolumnrow(self):
225247
df = pd.DataFrame({
226248
('c1', 0): {x: x for x in range(5)},

0 commit comments

Comments
 (0)