From c9173da891b741ceb9a4014f0b0c60878c133089 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Mon, 17 Jun 2013 23:04:18 -0400 Subject: [PATCH 1/2] ENH: add last element to repring of sequences --- pandas/core/common.py | 25 +++++++++++++------------ pandas/tests/test_common.py | 25 +++++++++++++++++++++++++ pandas/util/testing.py | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index a31c92caf4343..c3594fb86124e 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(seq[-1]) + 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..ad6ebf43408a9 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -3,6 +3,7 @@ import re import nose +from nose.tools import assert_equal import unittest from pandas import Series, DataFrame, date_range, DatetimeIndex @@ -266,6 +267,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 +288,29 @@ def test_ensure_platform_int(): pi = com._ensure_platform_int(x) assert(pi.dtype == np.int_) + +def test_pprint_max_seq_items(): + 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) + + with cf.option_context('display.max_seq_items', 2): + df = tm.makeCustomDataframe(2, 3, c_idx_nlevels=2) + mi = df.columns + s = ("MultiIndex\n[(u'C_l0_g0', u'C_l1_g0'), (u'C_l0_g1', u'C_l1_g1')," + " ..., ('C_l0_g2', '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): From 28babcec5bb67626ae0e0bafe92e4b0531d17bf4 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Tue, 18 Jun 2013 18:08:53 -0400 Subject: [PATCH 2/2] BUG/TST: fix failing test on py3 --- pandas/core/common.py | 2 +- pandas/tests/test_common.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index c3594fb86124e..ebbd87c9bf073 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1956,7 +1956,7 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds): body = u", ".join(r) if nitems < n: - body += u", ..., {0}".format(seq[-1]) + body += u", ..., {0}".format(pprint_thing(seq[-1], **kwds)) elif isinstance(seq, tuple) and n == 1: body += u',' diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index ad6ebf43408a9..8589e09bf038f 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -8,6 +8,7 @@ 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 @@ -290,6 +291,7 @@ def test_ensure_platform_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)) @@ -303,11 +305,37 @@ def test_pprint_max_seq_items(): 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 - s = ("MultiIndex\n[(u'C_l0_g0', u'C_l1_g0'), (u'C_l0_g1', u'C_l1_g1')," - " ..., ('C_l0_g2', 'C_l1_g2')]") + 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)