Skip to content

Commit 0f4f99a

Browse files
Chang Shewesm
Chang She
authored andcommitted
BUG: align Series repr when unicode index is present #1279
1 parent 10e43c6 commit 0f4f99a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pandas/core/format.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,14 @@ def to_string(self):
112112

113113
maxlen = max(len(x) for x in fmt_index)
114114
pad_space = min(maxlen, 60)
115-
result = ['%s %s' % (k.ljust(pad_space), v)
116-
for (k, v) in izip(fmt_index[1:], fmt_values)]
115+
116+
result = ['%s %s'] * len(fmt_values)
117+
for i, (k, v) in enumerate(izip(fmt_index[1:], fmt_values)):
118+
try:
119+
idx = k.ljust(pad_space + (len(k) - len(k.decode('utf-8'))))
120+
except UnicodeEncodeError:
121+
idx = k.ljust(pad_space)
122+
result[i] = result[i] % (idx, v)
117123

118124
if self.header and have_header:
119125
result.insert(0, fmt_index[0])

pandas/tests/test_format.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ def test_to_string_repr_unicode(self):
6969
# it works!
7070
repr(df)
7171

72+
idx = Index(['abc', u'\u03c3a', 'aegdvg'])
73+
ser = Series(np.random.randn(len(idx)), idx)
74+
rs = repr(ser).split('\n')
75+
line_len = len(rs[0])
76+
for line in rs[1:]:
77+
try:
78+
line = line.decode('utf-8')
79+
except:
80+
pass
81+
self.assert_(len(line) == line_len)
82+
7283
# it works even if sys.stdin in None
7384
sys.stdin = None
7485
repr(df)

0 commit comments

Comments
 (0)