Skip to content

Commit 4291857

Browse files
committed
Merge remote branch 'y-p/feature/limit_printing_long_seq'
* y-p/feature/limit_printing_long_seq: TST: add test for com.is_sequence ENH: add display.max_seq_items to limit len of pprinted long sequences
2 parents 2a6ee46 + dd7e3cc commit 4291857

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

pandas/core/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,8 +1677,13 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
16771677
rather then calling this directly.
16781678
"""
16791679
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
1680-
return fmt % ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
1681-
for e in seq[:len(seq)])
1680+
1681+
nitems = get_option("max_seq_items") or len(seq)
1682+
body = ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
1683+
for e in seq[:nitems])
1684+
if nitems < len(seq):
1685+
body+= ", ..."
1686+
return fmt % body
16821687

16831688

16841689
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_common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def test_is_sequence():
2626
assert(not is_seq(u"abcd"))
2727
assert(not is_seq(np.int64))
2828

29+
class A(object):
30+
def __getitem__(self):
31+
return 1
32+
33+
assert(not is_seq(A()))
2934

3035
def test_notnull():
3136
assert notnull(1.)

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)