diff --git a/pandas/core/common.py b/pandas/core/common.py index 03d43250f9265..3fb1f8bc08f5c 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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): diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 114210d75959b..e4eeea53e1636 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -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) @@ -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 diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index b3a0c5ee93699..7869d2627d581 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -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.) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index c31f4e3b8061d..c2a399b493d13 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -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): """