@@ -170,7 +170,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
170
170
else :
171
171
self .columns = frame .columns
172
172
173
- def to_string (self ):
173
+ def to_string (self , force_unicode = False ):
174
174
"""
175
175
Render a DataFrame to a console-friendly tabular output.
176
176
"""
@@ -209,10 +209,17 @@ def to_string(self):
209
209
else :
210
210
to_write .append (adjoin (1 , * stringified ))
211
211
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 :
214
222
to_write = [unicode (s ) for s in to_write ]
215
- break
216
223
217
224
self .buf .writelines (to_write )
218
225
@@ -358,9 +365,9 @@ def is_numeric_dtype(dtype):
358
365
fmt_columns = zip (* fmt_columns )
359
366
dtypes = self .frame .dtypes .values
360
367
need_leadsp = dict (zip (fmt_columns , map (is_numeric_dtype , dtypes )))
361
- str_columns = zip (* [[' %s' % y
368
+ str_columns = zip (* [[u ' %s' % y
362
369
if y not in formatters and need_leadsp [x ]
363
- else str ( y ) for y in x ]
370
+ else y for y in x ]
364
371
for x in fmt_columns ])
365
372
if self .sparsify :
366
373
str_columns = _sparsify (str_columns )
@@ -370,9 +377,9 @@ def is_numeric_dtype(dtype):
370
377
fmt_columns = self .columns .format ()
371
378
dtypes = self .frame .dtypes
372
379
need_leadsp = dict (zip (fmt_columns , map (is_numeric_dtype , dtypes )))
373
- str_columns = [[' %s' % x
380
+ str_columns = [[u ' %s' % x
374
381
if col not in formatters and need_leadsp [x ]
375
- else str ( x ) ]
382
+ else x ]
376
383
for col , x in zip (self .columns , fmt_columns )]
377
384
378
385
if self .show_index_names and self .has_index_names :
0 commit comments