diff --git a/pandas/core/common.py b/pandas/core/common.py index a31c92caf4343..ebbd87c9bf073 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1941,23 +1941,24 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds): bounds length of printed sequence, depending on options """ - if isinstance(seq,set): + if isinstance(seq, set): fmt = u"set([%s])" else: fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)" - nitems = get_option("max_seq_items") or len(seq) + n = len(seq) + nitems = get_option("max_seq_items") or n s = iter(seq) r = [] - for i in range(min(nitems,len(seq))): # handle sets, no slicing + for i in xrange(min(nitems, n)): # handle sets, no slicing r.append(pprint_thing(next(s), _nest_lvl + 1, **kwds)) - body = ", ".join(r) + body = u", ".join(r) - if nitems < len(seq): - body+= ", ..." - elif isinstance(seq,tuple) and len(seq) == 1: - body += ',' + if nitems < n: + body += u", ..., {0}".format(pprint_thing(seq[-1], **kwds)) + elif isinstance(seq, tuple) and n == 1: + body += u',' return fmt % body @@ -2010,10 +2011,10 @@ def pprint_thing(thing, _nest_lvl=0, escape_chars=None, default_escapes=False, result - unicode object on py2, str on py3. Always Unicode. """ - def as_escaped_unicode(thing,escape_chars=escape_chars): + def as_escaped_unicode(thing, escape_chars=escape_chars): # Unicode is fine, else we try to decode using utf-8 and 'replace' # if that's not it either, we have no way of knowing and the user - #should deal with it himself. + # should deal with it. try: result = unicode(thing) # we should try this first @@ -2043,12 +2044,12 @@ def as_escaped_unicode(thing,escape_chars=escape_chars): return unicode(thing) elif (isinstance(thing, dict) and _nest_lvl < get_option("display.pprint_nest_depth")): - result = _pprint_dict(thing, _nest_lvl,quote_strings=True) + result = _pprint_dict(thing, _nest_lvl, quote_strings=True) elif _is_sequence(thing) and _nest_lvl < \ get_option("display.pprint_nest_depth"): result = _pprint_seq(thing, _nest_lvl, escape_chars=escape_chars, quote_strings=quote_strings) - elif isinstance(thing,basestring) and quote_strings: + elif isinstance(thing, basestring) and quote_strings: if py3compat.PY3: fmt = "'%s'" else: diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index db01545fb3c9d..8589e09bf038f 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -3,10 +3,12 @@ import re import nose +from nose.tools import assert_equal import unittest from pandas import Series, DataFrame, date_range, DatetimeIndex from pandas.core.common import notnull, isnull +from pandas.util.py3compat import PY3 import pandas.core.common as com import pandas.util.testing as tm import pandas.core.config as cf @@ -266,6 +268,7 @@ def test_ensure_int32(): result = com._ensure_int32(values) assert(result.dtype == np.int32) + def test_ensure_platform_int(): # verify that when we create certain types of indices @@ -286,6 +289,56 @@ def test_ensure_platform_int(): pi = com._ensure_platform_int(x) assert(pi.dtype == np.int_) + +def test_pprint_max_seq_items(): + # test with a specific setting + with cf.option_context('display.max_seq_items', 3): + s = 'Int64Index([0, 1, 2, ..., 4], dtype=int64)' + res = repr(tm.makeIntIndex(5)) + assert_equal(s, res) + + s = 'Int64Index([0, 1, 2, ..., 10], dtype=int64)' + res = repr(tm.makeIntIndex(11)) + assert_equal(s, res) + + s = 'Int64Index([0, 1, 2], dtype=int64)' + res = repr(tm.makeIntIndex(3)) + assert_equal(s, res) + + s = 'Int64Index([0, 1, 2, ..., 3], dtype=int64)' + res = repr(tm.makeIntIndex(4)) + assert_equal(s, res) + + s = 'Int64Index([], dtype=int64)' + res = repr(tm.makeIntIndex(0)) + assert_equal(s, res) + + # test with the default + s = 'Int64Index([0, 1, 2, 3, 4], dtype=int64)' + res = repr(tm.makeIntIndex(5)) + assert_equal(s, res) + + s = 'Int64Index([0, 1], dtype=int64)' + res = repr(tm.makeIntIndex(2)) + assert_equal(s, res) + + s = 'Int64Index([], dtype=int64)' + res = repr(tm.makeIntIndex(0)) + assert_equal(s, res) + + # test multiindex + with cf.option_context('display.max_seq_items', 2): + df = tm.makeCustomDataframe(2, 3, c_idx_nlevels=2) + mi = df.columns + if PY3: + s = ("MultiIndex\n[('C_l0_g0', 'C_l1_g0'), ('C_l0_g1', 'C_l1_g1')," + " ..., ('C_l0_g2', 'C_l1_g2')]") + else: + s = ("MultiIndex\n[(u'C_l0_g0', u'C_l1_g0'), " + "(u'C_l0_g1', u'C_l1_g1'), ..., (u'C_l0_g2', u'C_l1_g2')]") + res = repr(mi) + assert_equal(s, res) + # TODO: fix this broken test # def test_console_encode(): diff --git a/pandas/util/testing.py b/pandas/util/testing.py index c297cfa554fa5..94631dd894324 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -302,7 +302,7 @@ def makeUnicodeIndex(k): def makeIntIndex(k): - return Index(range(k)) + return Index(np.arange(k)) def makeFloatIndex(k):