Skip to content

Commit 5ed523c

Browse files
committed
BUG: fix formatting of dict entries in Series/DataFrame. close #2144
1 parent fe5cc26 commit 5ed523c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pandas 0.9.1
4343
- Correctly compute/box datetime64 min/max values from Series.min/max (#2083)
4444
- Fix unstacking edge case with unrepresented groups (#2100)
4545
- Fix Series.str failures when using pipe pattern '|' (#2119)
46+
- Fix pretty-printing of dict entries in Series, DataFrame (#2144)
4647
4748
pandas 0.9.0
4849
============

pandas/core/common.py

+16
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,19 @@ def _pprint_seq(seq, _nest_lvl=0):
10531053
fmt = u"[%s]" if hasattr(seq, '__setitem__') else u"(%s)"
10541054
return fmt % ", ".join(pprint_thing(e, _nest_lvl + 1) for e in seq)
10551055

1056+
def _pprint_dict(seq, _nest_lvl=0):
1057+
"""
1058+
internal. pprinter for iterables. you should probably use pprint_thing()
1059+
rather then calling this directly.
1060+
"""
1061+
fmt = u"{%s}"
1062+
pairs = []
1063+
1064+
pfmt = u"%s: %s"
1065+
for k, v in seq.items():
1066+
pairs.append(pfmt % (repr(k), repr(v)))
1067+
return fmt % ", ".join(pairs)
1068+
10561069

10571070
def pprint_thing(thing, _nest_lvl=0):
10581071
"""
@@ -1077,6 +1090,9 @@ def pprint_thing(thing, _nest_lvl=0):
10771090
from pandas.core.format import print_config
10781091
if thing is None:
10791092
result = ''
1093+
elif (isinstance(thing, dict) and
1094+
_nest_lvl < print_config.pprint_nest_depth):
1095+
result = _pprint_dict(thing, _nest_lvl)
10801096
elif _is_sequence(thing) and _nest_lvl < print_config.pprint_nest_depth:
10811097
result = _pprint_seq(thing, _nest_lvl)
10821098
else:

pandas/tests/test_format.py

+5
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ def test_float_trim_zeros(self):
785785
self.assert_(('+10' in line) or skip)
786786
skip = False
787787

788+
def test_dict_entries(self):
789+
df = DataFrame({'A': [{'a':1, 'b':2}]})
790+
791+
val = df.to_string()
792+
self.assertTrue("{'a': 1, 'b': 2}" in val)
788793

789794
class TestSeriesFormatting(unittest.TestCase):
790795

0 commit comments

Comments
 (0)