Skip to content

ENH: add last element to repring of sequences #3941

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

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 13 additions & 12 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def makeUnicodeIndex(k):


def makeIntIndex(k):
return Index(range(k))
return Index(np.arange(k))


def makeFloatIndex(k):
Expand Down