Skip to content

Commit 1926b29

Browse files
author
y-p
committed
ENH: add display.max_seq_items to limit len of pprinted long sequences
1 parent 78b6659 commit 1926b29

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

pandas/core/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,13 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
16511651
rather then calling this directly.
16521652
"""
16531653
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
1654-
return fmt % ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
1655-
for e in seq[:len(seq)])
1654+
1655+
nitems = get_option("max_seq_items") or len(seq)
1656+
body = ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
1657+
for e in seq[:nitems])
1658+
if nitems < len(seq):
1659+
body+= ", ..."
1660+
return fmt % body
16561661

16571662

16581663
def _pprint_dict(seq, _nest_lvl=0):

pandas/core/config_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@
122122
if set to a float value, all float values smaller then the given threshold
123123
will be displayed as exactly 0 by repr and friends.
124124
"""
125+
pc_max_seq_items = """
126+
: int or None
125127
128+
when pretty-printing a long sequence, no more then `max_seq_items`
129+
will be printed. If items are ommitted, they will be denoted by the addition
130+
of "..." to the resulting string.
131+
132+
If set to None, the number of items to be printed is unlimited.
133+
"""
126134
with cf.config_prefix('display'):
127135
cf.register_option('precision', 7, pc_precision_doc, validator=is_int)
128136
cf.register_option('float_format', None, float_format_doc)
@@ -149,6 +157,7 @@
149157
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
150158
cf.register_option('line_width', 80, pc_line_width_doc)
151159
cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
160+
cf.register_option('max_seq_items', None, pc_max_seq_items)
152161

153162
tc_sim_interactive_doc = """
154163
: boolean

pandas/tests/test_format.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ def test_repr_chop_threshold(self):
114114
with option_context("display.chop_threshold", None ):
115115
self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')
116116

117+
def test_repr_obeys_max_seq_limit(self):
118+
import pandas.core.common as com
119+
120+
#unlimited
121+
reset_option("display.max_seq_items")
122+
self.assertTrue(len(com.pprint_thing(range(1000)))> 2000)
123+
124+
with option_context("display.max_seq_items",5):
125+
self.assertTrue(len(com.pprint_thing(range(1000)))< 100)
117126

118127
def test_repr_should_return_str(self):
119128
"""

0 commit comments

Comments
 (0)