Skip to content

Commit 02445f4

Browse files
author
y-p
committed
BUG: pprint_thing mishandles sets GH3294
closes #3294
1 parent b5c6e1c commit 02445f4

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ pandas 0.11.0
284284
a simple index (GH2893_)
285285
- Fix Python ascii file parsing when integer falls outside of floating point
286286
spacing (GH3258_)
287+
- fixed pretty priniting of sets (GH3294_)
287288

289+
.. _GH3294: https://github.com/pydata/pandas/issues/3294
288290
.. _GH622: https://github.com/pydata/pandas/issues/622
289291
.. _GH797: https://github.com/pydata/pandas/issues/797
290292
.. _GH1893: https://github.com/pydata/pandas/issues/1893

pandas/core/common.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,12 +1798,22 @@ def _pprint_seq(seq, _nest_lvl=0, **kwds):
17981798
"""
17991799
internal. pprinter for iterables. you should probably use pprint_thing()
18001800
rather then calling this directly.
1801+
1802+
bounds length of printed sequence, depending on options
18011803
"""
1802-
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
1804+
if isinstance(seq,set):
1805+
fmt = u"set([%s])"
1806+
else:
1807+
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
18031808

18041809
nitems = get_option("max_seq_items") or len(seq)
1805-
body = ", ".join(pprint_thing(e, _nest_lvl + 1, **kwds)
1806-
for e in seq[:nitems])
1810+
1811+
s = iter(seq)
1812+
r = []
1813+
for i in range(min(nitems,len(seq))): # handle sets, no slicing
1814+
r.append(pprint_thing(next(s), _nest_lvl + 1, **kwds))
1815+
body = ", ".join(r)
1816+
18071817
if nitems < len(seq):
18081818
body+= ", ..."
18091819
elif isinstance(seq,tuple) and len(seq) == 1:

pandas/tests/test_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ def test_pprint_thing():
308308
# escape embedded tabs in string
309309
# GH #2038
310310
assert not "\t" in pp_t("a\tb", escape_chars=("\t",))
311+
311312
assert(pp_t((1,)) == u'(1,)')
313+
assert("set" in pp_t(set([1,2,3]))) # it works
312314

313315
class TestTake(unittest.TestCase):
314316

@@ -684,7 +686,7 @@ def test_2d_float32(self):
684686
expected = arr.take(indexer, axis=1)
685687
expected[:, [2, 4]] = np.nan
686688
tm.assert_almost_equal(result, expected)
687-
689+
688690
def test_2d_datetime64(self):
689691
# 2005/01/01 - 2006/01/01
690692
arr = np.random.randint(11045376L, 11360736L, (5,3))*100000000000

0 commit comments

Comments
 (0)