Skip to content

Commit 6cee3e1

Browse files
committed
Add an option to switch back to info repr for large DataFrames
1 parent 1e72f9b commit 6cee3e1

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

doc/source/v0.13.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ Enhancements
611611
a truncated view of the table once it exceeds a certain size, rather
612612
than switching to the short info view (:issue:`4886`, :issue:`5550`).
613613
This makes the representation more consistent as small DataFrames get
614-
larger. To get the info view, call :meth:`DataFrame.info`.
614+
larger. To get the info view, call :meth:`DataFrame.info`, or restore
615+
the old behaviour with ``set_option('display.large_repr', 'info')``.
615616

616617
.. _whatsnew_0130.experimental:
617618

pandas/core/config_init.py

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@
173173
max_info_rows has been deprecated, as reprs no longer use the info view.
174174
"""
175175

176+
pc_large_repr_doc = """
177+
: 'truncate'/'info'
178+
179+
For DataFrames exceeding max_rows/max_cols, the repr (and HTML repr) can
180+
show a truncated table (the default from 0.13), or switch to the view from
181+
df.info() (the behaviour in earlier versions of pandas).
182+
"""
183+
176184
pc_mpl_style_doc = """
177185
: bool
178186
@@ -218,6 +226,8 @@ def mpl_style_cb(key):
218226
cf.register_option('max_colwidth', 50, max_colwidth_doc, validator=is_int)
219227
cf.register_option('max_columns', 20, pc_max_cols_doc,
220228
validator=is_instance_factory([type(None), int]))
229+
cf.register_option('large_repr', 'truncate', pc_large_repr_doc,
230+
validator=is_one_of_factory(['truncate', 'info']))
221231
cf.register_option('max_info_columns', 100, pc_max_info_cols_doc,
222232
validator=is_int)
223233
cf.register_option('colheader_justify', 'right', colheader_justify_doc,

pandas/core/frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ def _repr_fits_horizontal_(self, ignore_width=False):
429429

430430
return repr_width < width
431431

432+
def _info_repr(self):
433+
"""True if the repr should show the info view."""
434+
info_repr_option = (get_option("display.large_repr") == "info")
435+
return info_repr_option and not \
436+
(self._repr_fits_horizontal_() and self._repr_fits_vertical_())
437+
432438
def __unicode__(self):
433439
"""
434440
Return a string representation for a particular DataFrame
@@ -437,6 +443,10 @@ def __unicode__(self):
437443
py2/py3.
438444
"""
439445
buf = StringIO(u(""))
446+
if self._info_repr():
447+
self.info(buf=buf)
448+
return buf.getvalue()
449+
440450
max_rows = get_option("display.max_rows")
441451
max_cols = get_option("display.max_columns")
442452
if get_option("display.expand_frame_repr"):
@@ -464,6 +474,11 @@ def _repr_html_(self):
464474
if com.in_qtconsole():
465475
raise ValueError('Disable HTML output in QtConsole')
466476

477+
if self._info_repr():
478+
buf = StringIO(u(""))
479+
self.info(buf=buf)
480+
return '<pre>' + buf.getvalue() + '</pre>'
481+
467482
if get_option("display.notebook_repr_html"):
468483
max_rows = get_option("display.max_rows")
469484
max_cols = get_option("display.max_columns")

pandas/tests/test_format.py

+38
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def curpath():
3131
pth, _ = os.path.split(os.path.abspath(__file__))
3232
return pth
3333

34+
def has_info_repr(df):
35+
r = repr(df)
36+
return r.split('\n')[0].startswith("<class")
37+
3438
def has_horizontally_truncated_repr(df):
3539
r = repr(df)
3640
return any(l.strip().endswith('...') for l in r.splitlines())
@@ -1477,6 +1481,40 @@ def test_repr_html_long_and_wide(self):
14771481
df = pandas.DataFrame(dict((k,np.arange(1,1+h)) for k in np.arange(w)))
14781482
assert '...' in df._repr_html_()
14791483

1484+
def test_info_repr(self):
1485+
max_rows = get_option('display.max_rows')
1486+
max_cols = get_option('display.max_columns')
1487+
# Long
1488+
h, w = max_rows+1, max_cols-1
1489+
df = pandas.DataFrame(dict((k,np.arange(1,1+h)) for k in np.arange(w)))
1490+
assert has_vertically_truncated_repr(df)
1491+
with option_context('display.large_repr', 'info'):
1492+
assert has_info_repr(df)
1493+
1494+
# Wide
1495+
h, w = max_rows-1, max_cols+1
1496+
df = pandas.DataFrame(dict((k,np.arange(1,1+h)) for k in np.arange(w)))
1497+
assert has_vertically_truncated_repr(df)
1498+
with option_context('display.large_repr', 'info'):
1499+
assert has_info_repr(df)
1500+
1501+
def test_info_repr_html(self):
1502+
max_rows = get_option('display.max_rows')
1503+
max_cols = get_option('display.max_columns')
1504+
# Long
1505+
h, w = max_rows+1, max_cols-1
1506+
df = pandas.DataFrame(dict((k,np.arange(1,1+h)) for k in np.arange(w)))
1507+
assert '<class' not in df._repr_html_()
1508+
with option_context('display.large_repr', 'info'):
1509+
assert '<class' in df._repr_html_()
1510+
1511+
# Wide
1512+
h, w = max_rows-1, max_cols+1
1513+
df = pandas.DataFrame(dict((k,np.arange(1,1+h)) for k in np.arange(w)))
1514+
assert '<class' not in df._repr_html_()
1515+
with option_context('display.large_repr', 'info'):
1516+
assert '<class' in df._repr_html_()
1517+
14801518
def test_fake_qtconsole_repr_html(self):
14811519
def get_ipython():
14821520
return {'config':

0 commit comments

Comments
 (0)