Skip to content
Merged
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
12 changes: 10 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,23 @@ def _to_str_columns(self, force_unicode=False):

if not py3compat.PY3:
if force_unicode:
strcols = map(lambda col: map(unicode, col), strcols)
def make_unicode(x):
if isinstance(x, unicode):
return x
return x.decode('utf-8')
strcols = map(lambda col: map(make_unicode, col), strcols)
else:
# generally everything is plain strings, which has ascii
# encoding. problem is when there is a char with value over 127
# - everything then gets converted to unicode.
try:
map(lambda col: map(str, col), strcols)
except UnicodeError:
strcols = map(lambda col: map(unicode, col), strcols)
def make_unicode(x):
if isinstance(x, unicode):
return x
return x.decode('utf-8')
strcols = map(lambda col: map(make_unicode, col), strcols)

return strcols

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

try:
from StringIO import StringIO
except:
Expand Down Expand Up @@ -110,6 +112,12 @@ def test_to_string_unicode_three(self):
buf = StringIO()
dm.to_string(buf)

def test_to_string_force_unicode(self):
#given string with non-ascii characters
df = DataFrame([["aaää", 1], ["bbbb", 2]])
result = df.to_string(force_unicode=True)
self.assertEqual(result, u' 0 1\n0 aa\xe4\xe4 1\n1 bbbb 2')

def test_to_string_with_formatters(self):
df = DataFrame({'int': [1, 2, 3],
'float': [1.0, 2.0, 3.0],
Expand Down