File tree 2 files changed +28
-2
lines changed
2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -613,8 +613,12 @@ def get_col_type(dtype):
613
613
name = any (self .frame .columns .names )
614
614
for i , lev in enumerate (self .frame .index .levels ):
615
615
lev2 = lev .format (name = name )
616
- width = len (lev2 [0 ])
617
- lev3 = [' ' * width ] * clevels + lev2
616
+ blank = ' ' * len (lev2 [0 ])
617
+ lev3 = [blank ] * clevels
618
+ for level_idx , group in itertools .groupby (
619
+ self .frame .index .labels [i ]):
620
+ count = len (list (group ))
621
+ lev3 .extend ([lev2 [level_idx ]] + [blank ] * (count - 1 ))
618
622
strcols .insert (i , lev3 )
619
623
620
624
if column_format is None :
Original file line number Diff line number Diff line change @@ -2194,6 +2194,28 @@ def test_to_latex_multiindex(self):
2194
2194
x & y & a \\
2195
2195
\bottomrule
2196
2196
\end{tabular}
2197
+ """
2198
+ self .assertEqual (result , expected )
2199
+
2200
+ df = DataFrame .from_dict ({
2201
+ ('c1' , 0 ): pd .Series ({x : x for x in range (4 )}),
2202
+ ('c1' , 1 ): pd .Series ({x : x + 4 for x in range (4 )}),
2203
+ ('c2' , 0 ): pd .Series ({x : x for x in range (4 )}),
2204
+ ('c2' , 1 ): pd .Series ({x : x + 4 for x in range (4 )}),
2205
+ ('c3' , 0 ): pd .Series ({x : x for x in range (4 )}),
2206
+ }).T
2207
+ result = df .to_latex ()
2208
+ expected = r"""\begin{tabular}{llrrrr}
2209
+ \toprule
2210
+ & & 0 & 1 & 2 & 3 \\
2211
+ \midrule
2212
+ c1 & 0 & 0 & 1 & 2 & 3 \\
2213
+ & 1 & 4 & 5 & 6 & 7 \\
2214
+ c2 & 0 & 0 & 1 & 2 & 3 \\
2215
+ & 1 & 4 & 5 & 6 & 7 \\
2216
+ c3 & 0 & 0 & 1 & 2 & 3 \\
2217
+ \bottomrule
2218
+ \end{tabular}
2197
2219
"""
2198
2220
self .assertEqual (result , expected )
2199
2221
You can’t perform that action at this time.
0 commit comments