Skip to content

Commit 074e88b

Browse files
lodagrowesm
authored andcommitted
Adding clever screen render mode.
1 parent b2138d4 commit 074e88b

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

pandas/core/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
359359
max_columns : int
360360
max_rows and max_columns are used in __repr__() methods to decide if
361361
to_string() or info() is used to render an object to a string.
362+
Either one, or both can be set to 0 (experimental). Pandas will figure
363+
out how big the terminal is and will not display more rows or/and
364+
columns that can fit on it.
362365
"""
363366
global _float_format, _column_space, _max_rows, _max_columns
364367
if precision is not None:

pandas/core/frame.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pandas.core.internals import BlockManager, make_block, form_blocks
3333
from pandas.core.series import Series, _is_bool_indexer
3434
from pandas.util import py3compat
35+
from pandas.util.terminal import get_terminal_size
3536
import pandas.core.nanops as nanops
3637
import pandas.core.common as com
3738
import pandas.core.datetools as datetools
@@ -317,14 +318,33 @@ def __repr__(self):
317318
"""
318319
Return a string representation for a particular DataFrame
319320
"""
320-
buf = StringIO()
321-
if len(self.index) < com._max_rows and \
322-
len(self.columns) <= com._max_columns:
323-
self.to_string(buf=buf)
324-
else:
325-
self.info(buf=buf, verbose=self._verbose_info)
321+
terminal_width, terminal_height = get_terminal_size()
322+
max_rows = terminal_height if com._max_rows == 0 else com._max_rows
323+
max_columns = com._max_columns
326324

327-
return buf.getvalue()
325+
if max_columns > 0:
326+
buf = StringIO()
327+
if len(self.index) < max_rows and \
328+
len(self.columns) <= max_columns:
329+
self.to_string(buf=buf)
330+
else:
331+
self.info(buf=buf, verbose=self._verbose_info)
332+
return buf.getvalue()
333+
else:
334+
if len(self.index) > max_rows:
335+
buf = StringIO()
336+
self.info(buf=buf, verbose=self._verbose_info)
337+
return buf.getvalue()
338+
else:
339+
buf = StringIO()
340+
self.to_string(buf=buf)
341+
value = buf.getvalue()
342+
if max([len(l) for l in value.split('\n')]) <= terminal_width:
343+
return value
344+
else:
345+
buf = StringIO()
346+
self.info(buf=buf, verbose=self._verbose_info)
347+
return buf.getvalue()
328348

329349
def __iter__(self):
330350
"""

pandas/core/series.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pandas.core.index import Index, MultiIndex, _ensure_index
2121
from pandas.core.indexing import _SeriesIndexer, _maybe_droplevels
2222
from pandas.util import py3compat
23+
from pandas.util.terminal import get_terminal_size
2324
import pandas.core.common as common
2425
import pandas.core.datetools as datetools
2526
import pandas.core.nanops as nanops
@@ -419,8 +420,10 @@ def __setslice__(self, i, j, value):
419420

420421
def __repr__(self):
421422
"""Clean string representation of a Series"""
422-
if len(self.index) > common._max_rows:
423-
result = self._tidy_repr(min(30, common._max_rows))
423+
width, height = get_terminal_size()
424+
max_rows = height if common._max_rows == 0 else common._max_rows
425+
if len(self.index) > max_rows:
426+
result = self._tidy_repr(min(30, max_rows - 4))
424427
elif len(self.index) > 0:
425428
result = self._get_repr(print_header=True,
426429
length=len(self) > 50,

0 commit comments

Comments
 (0)