@@ -840,7 +840,34 @@ def _get_column_name_list(self):
840
840
841
841
842
842
def format_array (values , formatter , float_format = None , na_rep = 'NaN' ,
843
- digits = None , space = None , justify = 'right' , decimal = '.' ):
843
+ digits = None , space = None , justify = 'right' , decimal = '.' ,
844
+ leading_space = None ):
845
+ """
846
+ Format an array for printing.
847
+
848
+ Parameters
849
+ ----------
850
+ values
851
+ formatter
852
+ float_format
853
+ na_rep
854
+ digits
855
+ space
856
+ justify
857
+ decimal
858
+ leading_space : bool, optional
859
+ Whether the array should be formatted with a leading space.
860
+ When an array as a column of a Series or DataFrame, we do want
861
+ the leading space to pad between columns.
862
+
863
+ When formatting an Index subclass
864
+ (e.g. IntervalIndex._format_native_types), we don't want the
865
+ leading space since it should be left-aligned.
866
+
867
+ Returns
868
+ -------
869
+ List[str]
870
+ """
844
871
845
872
if is_datetime64_dtype (values .dtype ):
846
873
fmt_klass = Datetime64Formatter
@@ -868,7 +895,8 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
868
895
869
896
fmt_obj = fmt_klass (values , digits = digits , na_rep = na_rep ,
870
897
float_format = float_format , formatter = formatter ,
871
- space = space , justify = justify , decimal = decimal )
898
+ space = space , justify = justify , decimal = decimal ,
899
+ leading_space = leading_space )
872
900
873
901
return fmt_obj .get_result ()
874
902
@@ -877,7 +905,7 @@ class GenericArrayFormatter(object):
877
905
878
906
def __init__ (self , values , digits = 7 , formatter = None , na_rep = 'NaN' ,
879
907
space = 12 , float_format = None , justify = 'right' , decimal = '.' ,
880
- quoting = None , fixed_width = True ):
908
+ quoting = None , fixed_width = True , leading_space = None ):
881
909
self .values = values
882
910
self .digits = digits
883
911
self .na_rep = na_rep
@@ -888,6 +916,7 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN',
888
916
self .decimal = decimal
889
917
self .quoting = quoting
890
918
self .fixed_width = fixed_width
919
+ self .leading_space = leading_space
891
920
892
921
def get_result (self ):
893
922
fmt_values = self ._format_strings ()
@@ -927,7 +956,9 @@ def _format(x):
927
956
vals = vals .values
928
957
929
958
is_float_type = lib .map_infer (vals , is_float ) & notna (vals )
930
- leading_space = is_float_type .any ()
959
+ leading_space = self .leading_space
960
+ if leading_space is None :
961
+ leading_space = is_float_type .any ()
931
962
932
963
fmt_values = []
933
964
for i , v in enumerate (vals ):
@@ -936,7 +967,13 @@ def _format(x):
936
967
elif is_float_type [i ]:
937
968
fmt_values .append (float_format (v ))
938
969
else :
939
- fmt_values .append (u' {v}' .format (v = _format (v )))
970
+ if leading_space is False :
971
+ # False specifically, so that the default is
972
+ # to include a space if we get here.
973
+ tpl = u'{v}'
974
+ else :
975
+ tpl = u' {v}'
976
+ fmt_values .append (tpl .format (v = _format (v )))
940
977
941
978
return fmt_values
942
979
@@ -1135,7 +1172,8 @@ def _format_strings(self):
1135
1172
formatter ,
1136
1173
float_format = self .float_format ,
1137
1174
na_rep = self .na_rep , digits = self .digits ,
1138
- space = self .space , justify = self .justify )
1175
+ space = self .space , justify = self .justify ,
1176
+ leading_space = self .leading_space )
1139
1177
return fmt_values
1140
1178
1141
1179
0 commit comments