@@ -840,7 +840,34 @@ def _get_column_name_list(self):
840840
841841
842842def 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+ """
844871
845872 if is_datetime64_dtype (values .dtype ):
846873 fmt_klass = Datetime64Formatter
@@ -868,7 +895,8 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
868895
869896 fmt_obj = fmt_klass (values , digits = digits , na_rep = na_rep ,
870897 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 )
872900
873901 return fmt_obj .get_result ()
874902
@@ -877,7 +905,7 @@ class GenericArrayFormatter(object):
877905
878906 def __init__ (self , values , digits = 7 , formatter = None , na_rep = 'NaN' ,
879907 space = 12 , float_format = None , justify = 'right' , decimal = '.' ,
880- quoting = None , fixed_width = True ):
908+ quoting = None , fixed_width = True , leading_space = None ):
881909 self .values = values
882910 self .digits = digits
883911 self .na_rep = na_rep
@@ -888,6 +916,7 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN',
888916 self .decimal = decimal
889917 self .quoting = quoting
890918 self .fixed_width = fixed_width
919+ self .leading_space = leading_space
891920
892921 def get_result (self ):
893922 fmt_values = self ._format_strings ()
@@ -927,7 +956,9 @@ def _format(x):
927956 vals = vals .values
928957
929958 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 ()
931962
932963 fmt_values = []
933964 for i , v in enumerate (vals ):
@@ -936,7 +967,13 @@ def _format(x):
936967 elif is_float_type [i ]:
937968 fmt_values .append (float_format (v ))
938969 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 )))
940977
941978 return fmt_values
942979
@@ -1135,7 +1172,8 @@ def _format_strings(self):
11351172 formatter ,
11361173 float_format = self .float_format ,
11371174 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 )
11391177 return fmt_values
11401178
11411179
0 commit comments