Skip to content

Commit f48a954

Browse files
author
y-p
committed
ENH: optimize _repr_fits_horizontal_, to_string only on slice of interest
1 parent fffd92f commit f48a954

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pandas/core/frame.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,18 @@ def _repr_fits_horizontal_(self):
626626
return False
627627

628628
buf = StringIO()
629-
self.to_string(buf=buf)
629+
630+
# only care about the stuff we'll actually print out
631+
# and to_string on entire frame may be expensive
632+
d = self
633+
max_rows = get_option("display.max_rows")
634+
if not (height is None and max_rows is None):
635+
# min of two, where one may be None
636+
height = height or max_rows +1
637+
max_rows = max_rows or height +1
638+
d=d.iloc[:min(max_rows, height)]
639+
640+
d.to_string(buf=buf)
630641
value = buf.getvalue()
631642
repr_width = max([len(l) for l in value.split('\n')])
632643
return repr_width <= width

0 commit comments

Comments
 (0)