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

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Mar 10, 2013
Jump to file or symbol
Failed to load files and symbols.
+30 −2
Split
View
@@ -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):
@@ -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
@@ -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.)
@@ -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):
"""