1
1
from itertools import izip
2
+ import sys
2
3
3
4
try :
4
5
from StringIO import StringIO
@@ -480,15 +481,30 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN',
480
481
self .justify = justify
481
482
482
483
def get_result (self ):
483
- if self ._have_unicode ():
484
+ if self ._conv_unicode ():
484
485
fmt_values = self ._format_strings (use_unicode = True )
485
486
else :
486
487
fmt_values = self ._format_strings (use_unicode = False )
487
488
488
489
return _make_fixed_width (fmt_values , self .justify )
489
490
490
- def _have_unicode (self ):
491
- mask = lib .map_infer (self .values , lambda x : isinstance (x , unicode ))
491
+ def _conv_unicode (self ):
492
+ #check if any needs and can be converted to nonascii encoding
493
+ def _nonascii (x ):
494
+ if isinstance (x , unicode ):
495
+ return True
496
+ try :
497
+ if isinstance (x , str ):
498
+ x .decode ('ascii' )
499
+ return False
500
+ except UnicodeError :
501
+ try :
502
+ x .decode (print_config .encoding )
503
+ return True
504
+ except UnicodeError :
505
+ return False
506
+ return False
507
+ mask = lib .map_infer (self .values , _nonascii )
492
508
return mask .any ()
493
509
494
510
def _format_strings (self , use_unicode = False ):
@@ -501,7 +517,9 @@ def _format_strings(self, use_unicode=False):
501
517
float_format = self .float_format
502
518
503
519
if use_unicode :
504
- formatter = _stringify if self .formatter is None else self .formatter
520
+ def _strify (x ):
521
+ return _stringify (x , print_config .encoding )
522
+ formatter = _strify if self .formatter is None else self .formatter
505
523
else :
506
524
formatter = str if self .formatter is None else self .formatter
507
525
@@ -668,7 +686,7 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
668
686
max_columns = None , colheader_justify = None ,
669
687
max_colwidth = None , notebook_repr_html = None ,
670
688
date_dayfirst = None , date_yearfirst = None ,
671
- multi_sparse = None ):
689
+ multi_sparse = None , encoding = None ):
672
690
"""
673
691
Alter default behavior of DataFrame.toString
674
692
@@ -716,6 +734,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
716
734
print_config .date_yearfirst = date_yearfirst
717
735
if multi_sparse is not None :
718
736
print_config .multi_sparse = multi_sparse
737
+ if encoding is not None :
738
+ print_config .encoding = encoding
719
739
720
740
def reset_printoptions ():
721
741
print_config .reset ()
@@ -846,6 +866,9 @@ def __init__(self):
846
866
self .date_dayfirst = False
847
867
self .date_yearfirst = False
848
868
self .multi_sparse = True
869
+ self .encoding = sys .getdefaultencoding ()
870
+ if self .encoding == 'ascii' :
871
+ self .encoding = 'UTF8'
849
872
850
873
def reset (self ):
851
874
self .__init__ ()
0 commit comments