@@ -268,7 +268,8 @@ class TableSchemaFormatter(BaseFormatter):
268
268
max_seq_items = max_seq_items )
269
269
270
270
271
- def format_object_summary (obj , formatter , is_justify = True , name = None ):
271
+ def format_object_summary (obj , formatter , is_justify = True ,
272
+ name = None , is_multi = False ):
272
273
"""
273
274
Return the formatted obj as a unicode string
274
275
@@ -280,8 +281,10 @@ def format_object_summary(obj, formatter, is_justify=True, name=None):
280
281
string formatter for an element
281
282
is_justify : boolean
282
283
should justify the display
283
- name : name, optiona
284
+ name : name, optional
284
285
defaults to the class name of the obj
286
+ is_multi : bool, default False
287
+ Is ``obj`` a :class:`MultiIndex` or not
285
288
286
289
Returns
287
290
-------
@@ -301,7 +304,7 @@ def format_object_summary(obj, formatter, is_justify=True, name=None):
301
304
space2 = "\n %s" % (' ' * (len (name ) + 2 ))
302
305
303
306
n = len (obj )
304
- sep = ','
307
+ sep = ',' if not is_multi else ( ', \n ' + ' ' * len ( name ))
305
308
max_seq_items = get_option ('display.max_seq_items' ) or n
306
309
307
310
# are we a truncated display
@@ -327,10 +330,10 @@ def best_len(values):
327
330
328
331
if n == 0 :
329
332
summary = '[], '
330
- elif n == 1 :
333
+ elif n == 1 and not is_multi :
331
334
first = formatter (obj [0 ])
332
335
summary = '[%s], ' % first
333
- elif n == 2 :
336
+ elif n == 2 and not is_multi :
334
337
first = formatter (obj [0 ])
335
338
last = formatter (obj [- 1 ])
336
339
summary = '[%s, %s], ' % (first , last )
@@ -346,15 +349,16 @@ def best_len(values):
346
349
347
350
# adjust all values to max length if needed
348
351
if is_justify :
349
-
350
- # however, if we are not truncated and we are only a single
351
- # line, then don't justify
352
- if (is_truncated or
353
- not (len (', ' .join (head )) < display_width and
354
- len (', ' .join (tail )) < display_width )):
355
- max_len = max (best_len (head ), best_len (tail ))
356
- head = [x .rjust (max_len ) for x in head ]
357
- tail = [x .rjust (max_len ) for x in tail ]
352
+ head , tail = _justify (head , tail , display_width , best_len ,
353
+ is_truncated , is_multi )
354
+ if is_multi :
355
+ max_space = display_width - len (space2 )
356
+ item = tail [0 ]
357
+ for i in reversed (range (1 , len (item ) + 1 )):
358
+ if len (_pprint_seq (item , max_seq_items = i )) < max_space :
359
+ break
360
+ head = [_pprint_seq (x , max_seq_items = i ) for x in head ]
361
+ tail = [_pprint_seq (x , max_seq_items = i ) for x in tail ]
358
362
359
363
summary = ""
360
364
line = space2
@@ -380,7 +384,7 @@ def best_len(values):
380
384
summary += line
381
385
summary += '],'
382
386
383
- if len (summary ) > (display_width ):
387
+ if len (summary ) > (display_width ) or is_multi :
384
388
summary += space1
385
389
else : # one row
386
390
summary += ' '
@@ -391,6 +395,52 @@ def best_len(values):
391
395
return summary
392
396
393
397
398
+ def _justify (head , tail , display_width , best_len ,
399
+ is_truncated = False , is_multi = False ):
400
+ """
401
+ Justify each item in head and tail, so they align properly.
402
+ """
403
+ if is_multi :
404
+ max_length = _max_level_item_length (head + tail )
405
+ head = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
406
+ for seq in head ]
407
+ tail = [tuple (x .rjust (max_len ) for x , max_len in zip (seq , max_length ))
408
+ for seq in tail ]
409
+ elif (is_truncated or not (len (', ' .join (head )) < display_width and
410
+ len (', ' .join (tail )) < display_width )):
411
+ max_length = max (best_len (head ), best_len (tail ))
412
+ head = [x .rjust (max_length ) for x in head ]
413
+ tail = [x .rjust (max_length ) for x in tail ]
414
+
415
+ return head , tail
416
+
417
+
418
+ def _max_level_item_length (seq ):
419
+ """
420
+ For each position for the sequences in ``seq``, find the largest length.
421
+
422
+ Used for justifying individual values in a :class:`pandas.MultiIndex`.
423
+
424
+ Parameters
425
+ ----------
426
+ seq : list-like of list-likes of strings
427
+
428
+ Returns
429
+ -------
430
+ max_length : list of ints
431
+
432
+ Examples
433
+ --------
434
+ >>> _max_level_item_length([['s', 'ab'], ['abc', 'a']])
435
+ [3, 2]
436
+ """
437
+ max_length = [0 ] * len (seq [0 ])
438
+ for inner_seq in seq :
439
+ length = [len (item ) for item in inner_seq ]
440
+ max_length = [max (x , y ) for x , y in zip (max_length , length )]
441
+ return max_length
442
+
443
+
394
444
def format_object_attrs (obj ):
395
445
"""
396
446
Return a list of tuples of the (attr, formatted_value)
0 commit comments