@@ -437,17 +437,24 @@ def _format_data(self):
437
437
from pandas .core .format import get_console_size
438
438
display_width , _ = get_console_size ()
439
439
if display_width is None :
440
- display_width = get_option ('display.width' )
440
+ display_width = get_option ('display.width' ) or 80
441
441
442
442
space1 = "\n %s" % (' ' * (len (self .__class__ .__name__ ) + 1 ))
443
443
space2 = "\n %s" % (' ' * (len (self .__class__ .__name__ ) + 2 ))
444
444
445
+ n = len (self )
445
446
sep = ','
446
447
max_seq_items = get_option ('display.max_seq_items' )
447
448
formatter = self ._formatter_func
448
- needs_justify = self .inferred_type in ['string' ,'categorical' ]
449
+
450
+ # do we want to justify (only do so for non-objects)
451
+ is_justify = not (self .inferred_type == 'string' or self .inferred_type == 'categorical' and is_object_dtype (self .categories ))
452
+
453
+ # are we a truncated display
454
+ is_truncated = n > max_seq_items
449
455
450
456
def _extend_line (s , line , value , display_width , next_line_prefix ):
457
+
451
458
if len (line .rstrip ()) + len (value .rstrip ()) >= display_width :
452
459
s += line .rstrip ()
453
460
line = next_line_prefix
@@ -460,7 +467,6 @@ def best_len(values):
460
467
else :
461
468
return 0
462
469
463
- n = len (self )
464
470
if n == 0 :
465
471
summary = '[], '
466
472
elif n == 1 :
@@ -471,26 +477,23 @@ def best_len(values):
471
477
last = formatter (self [- 1 ])
472
478
summary = '[%s, %s], ' % (first , last )
473
479
else :
480
+
474
481
if n > max_seq_items :
475
482
n = min (max_seq_items // 2 ,10 )
476
483
head = [ formatter (x ) for x in self [:n ] ]
477
484
tail = [ formatter (x ) for x in self [- n :] ]
478
- summary_insert = True
479
485
else :
480
486
head = []
481
487
tail = [ formatter (x ) for x in self ]
482
- summary_insert = False
483
-
484
- if needs_justify :
485
- justify = False
486
- else :
487
- justify = True
488
488
489
489
# adjust all values to max length if needed
490
- if justify :
491
- max_len = max (best_len (head ), best_len (tail ))
492
- head = [x .rjust (max_len ) for x in head ]
493
- tail = [x .rjust (max_len ) for x in tail ]
490
+ if is_justify :
491
+
492
+ # however, if we are not truncated and we are only a single line, then don't justify
493
+ if is_truncated or not (len (', ' .join (head )) < display_width and len (', ' .join (tail )) < display_width ):
494
+ max_len = max (best_len (head ), best_len (tail ))
495
+ head = [x .rjust (max_len ) for x in head ]
496
+ tail = [x .rjust (max_len ) for x in tail ]
494
497
495
498
summary = ""
496
499
line = space2
@@ -499,7 +502,7 @@ def best_len(values):
499
502
word = head [i ] + sep + ' '
500
503
summary , line = _extend_line (summary , line , word ,
501
504
display_width , space2 )
502
- if summary_insert :
505
+ if is_truncated :
503
506
summary += line + space2 + '...'
504
507
line = space2
505
508
0 commit comments