Skip to content

Commit f925264

Browse files
committed
BUG: #680 rears again. cut off another hydra head
1 parent 05b2789 commit f925264

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

pandas/core/common.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,11 @@ def set_eng_float_format(precision=None, accuracy=3, use_eng_prefix=False):
511511

512512
def _stringify(col):
513513
# unicode workaround
514-
if isinstance(col, tuple):
515-
return str(col)
516-
else:
517-
return '%s' % console_encode(col)
514+
return unicode(col)
515+
#if isinstance(col, tuple):
516+
# return str(col)
517+
#else:
518+
# return '%s' % console_encode(col)
518519

519520
def _float_format_default(v, width=None):
520521
"""
@@ -818,7 +819,6 @@ def load(path):
818819
finally:
819820
f.close()
820821

821-
822822
def console_encode(value):
823823
if py3compat.PY3 or not isinstance(value, unicode):
824824
return value

pandas/core/format.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
170170
else:
171171
self.columns = frame.columns
172172

173-
def to_string(self):
173+
def to_string(self, force_unicode=False):
174174
"""
175175
Render a DataFrame to a console-friendly tabular output.
176176
"""
@@ -209,10 +209,17 @@ def to_string(self):
209209
else:
210210
to_write.append(adjoin(1, *stringified))
211211

212-
for s in to_write:
213-
if isinstance(s, unicode):
212+
if force_unicode:
213+
to_write = [unicode(s) for s in to_write]
214+
else:
215+
# generally everything is plain strings, which has ascii encoding.
216+
# problem is when there is a char with value over 127 - everything
217+
# then gets converted to unicode.
218+
try:
219+
for s in to_write:
220+
str(s)
221+
except UnicodeError:
214222
to_write = [unicode(s) for s in to_write]
215-
break
216223

217224
self.buf.writelines(to_write)
218225

@@ -358,9 +365,9 @@ def is_numeric_dtype(dtype):
358365
fmt_columns = zip(*fmt_columns)
359366
dtypes = self.frame.dtypes.values
360367
need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes)))
361-
str_columns = zip(*[[' %s' % y
368+
str_columns = zip(*[[u' %s' % y
362369
if y not in formatters and need_leadsp[x]
363-
else str(y) for y in x]
370+
else y for y in x]
364371
for x in fmt_columns])
365372
if self.sparsify:
366373
str_columns = _sparsify(str_columns)
@@ -370,9 +377,9 @@ def is_numeric_dtype(dtype):
370377
fmt_columns = self.columns.format()
371378
dtypes = self.frame.dtypes
372379
need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes)))
373-
str_columns = [[' %s' % x
380+
str_columns = [[u' %s' % x
374381
if col not in formatters and need_leadsp[x]
375-
else str(x)]
382+
else x]
376383
for col, x in zip(self.columns, fmt_columns)]
377384

378385
if self.show_index_names and self.has_index_names:

pandas/tests/test_frame.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1858,13 +1858,7 @@ def test_to_string_unicode_two(self):
18581858
def test_to_string_with_formatters_unicode(self):
18591859
df = DataFrame({u'c/\u03c3':[1,2,3]})
18601860
result = df.to_string(formatters={u'c/\u03c3': lambda x: '%s' % x})
1861-
cp437 = u' c/\u03c3\n0 1 \n1 2 \n2 3 '.encode('cp437', 'ignore')
1862-
if py3compat.PY3:
1863-
self.assertEqual(result, u' c/\u03c3\n0 1 \n1 2 \n2 3 ')
1864-
else:
1865-
assert(result in
1866-
(' c/\xcf\x83\n0 1 \n1 2 \n2 3 ', cp437,
1867-
' c/?\n0 1 \n1 2 \n2 3 ' ))
1861+
self.assertEqual(result, u' c/\u03c3\n0 1 \n1 2 \n2 3 ')
18681862

18691863
def test_to_string_buffer_all_unicode(self):
18701864
buf = StringIO()
@@ -1878,6 +1872,10 @@ def test_to_string_buffer_all_unicode(self):
18781872
# this should work
18791873
''.join(buf.buflist)
18801874

1875+
def test_unicode_problem_decoding_as_ascii(self):
1876+
dm = DataFrame({u'c/\u03c3': Series({'test':np.NaN})})
1877+
unicode(dm.to_string())
1878+
18811879
def test_head_tail(self):
18821880
assert_frame_equal(self.frame.head(), self.frame[:5])
18831881
assert_frame_equal(self.frame.tail(), self.frame[-5:])

0 commit comments

Comments
 (0)