Skip to content

ENH: add display.max_seq_items to limit len of pprinted long sequences #2979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Mar 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,13 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
rather then calling this directly.
"""
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
return fmt % ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
for e in seq[:len(seq)])

nitems = get_option("max_seq_items") or len(seq)
body = ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
for e in seq[:nitems])
if nitems < len(seq):
body+= ", ..."
return fmt % body


def _pprint_dict(seq, _nest_lvl=0):
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@
if set to a float value, all float values smaller then the given threshold
will be displayed as exactly 0 by repr and friends.
"""
pc_max_seq_items = """
: int or None

when pretty-printing a long sequence, no more then `max_seq_items`
will be printed. If items are ommitted, they will be denoted by the addition
of "..." to the resulting string.

If set to None, the number of items to be printed is unlimited.
"""
with cf.config_prefix('display'):
cf.register_option('precision', 7, pc_precision_doc, validator=is_int)
cf.register_option('float_format', None, float_format_doc)
Expand All @@ -149,6 +157,7 @@
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
cf.register_option('line_width', 80, pc_line_width_doc)
cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
cf.register_option('max_seq_items', None, pc_max_seq_items)

tc_sim_interactive_doc = """
: boolean
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def test_is_sequence():
assert(not is_seq(u"abcd"))
assert(not is_seq(np.int64))

class A(object):
def __getitem__(self):
return 1

assert(not is_seq(A()))

def test_notnull():
assert notnull(1.)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ def test_repr_chop_threshold(self):
with option_context("display.chop_threshold", None ):
self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')

def test_repr_obeys_max_seq_limit(self):
import pandas.core.common as com

#unlimited
reset_option("display.max_seq_items")
self.assertTrue(len(com.pprint_thing(range(1000)))> 2000)

with option_context("display.max_seq_items",5):
self.assertTrue(len(com.pprint_thing(range(1000)))< 100)

def test_repr_should_return_str(self):
"""
Expand Down