@@ -717,20 +717,11 @@ def _format_col(self, i):
717
717
frame = self .tr_frame
718
718
formatter = self ._get_formatter (i )
719
719
values_to_format = frame .iloc [:, i ]._formatting_values ()
720
- if formatter :
721
- try :
722
- fmt_values = [formatter (x ) for x in values_to_format ]
723
- except AttributeError :
724
- # assume we have np.datetime64 array
725
- values_to_format = DatetimeIndex (values_to_format )
726
- fmt_values = [formatter (x ) for x in values_to_format ]
727
- return _make_fixed_width (fmt_values , self .justify )
728
-
729
- else :
730
- return format_array (
731
- values_to_format , formatter = None ,
732
- float_format = self .float_format , na_rep = self .na_rep ,
733
- space = self .col_space , decimal = self .decimal )
720
+ shortcut = formatter is not None
721
+ return format_array (values_to_format , formatter ,
722
+ float_format = self .float_format , na_rep = self .na_rep ,
723
+ space = self .col_space , decimal = self .decimal ,
724
+ shortcut = shortcut )
734
725
735
726
def to_html (self , classes = None , notebook = False , border = None ):
736
727
"""
@@ -867,7 +858,7 @@ def _get_column_name_list(self):
867
858
868
859
def format_array (values , formatter , float_format = None , na_rep = 'NaN' ,
869
860
digits = None , space = None , justify = 'right' , decimal = '.' ,
870
- leading_space = None ):
861
+ leading_space = None , shortcut = False ):
871
862
"""
872
863
Format an array for printing.
873
864
@@ -889,6 +880,9 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
889
880
When formatting an Index subclass
890
881
(e.g. IntervalIndex._format_native_types), we don't want the
891
882
leading space since it should be left-aligned.
883
+ shortcut : bool, optional, default False
884
+ Whether to shortcut the formatting options. Used when specifying
885
+ custom formatters in to_string, to_latex and to_html
892
886
893
887
Returns
894
888
-------
@@ -922,7 +916,7 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
922
916
fmt_obj = fmt_klass (values , digits = digits , na_rep = na_rep ,
923
917
float_format = float_format , formatter = formatter ,
924
918
space = space , justify = justify , decimal = decimal ,
925
- leading_space = leading_space )
919
+ leading_space = leading_space , shortcut = shortcut )
926
920
927
921
return fmt_obj .get_result ()
928
922
@@ -931,7 +925,8 @@ class GenericArrayFormatter(object):
931
925
932
926
def __init__ (self , values , digits = 7 , formatter = None , na_rep = 'NaN' ,
933
927
space = 12 , float_format = None , justify = 'right' , decimal = '.' ,
934
- quoting = None , fixed_width = True , leading_space = None ):
928
+ quoting = None , fixed_width = True , leading_space = None ,
929
+ shortcut = False ):
935
930
self .values = values
936
931
self .digits = digits
937
932
self .na_rep = na_rep
@@ -943,12 +938,17 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN',
943
938
self .quoting = quoting
944
939
self .fixed_width = fixed_width
945
940
self .leading_space = leading_space
941
+ self .shortcut = shortcut
946
942
947
943
def get_result (self ):
948
944
fmt_values = self ._format_strings ()
949
945
return _make_fixed_width (fmt_values , self .justify )
950
946
951
947
def _format_strings (self ):
948
+ # shortcut
949
+ if self .formatter is not None and self .shortcut :
950
+ return [self .formatter (x ) for x in self .values ]
951
+
952
952
if self .float_format is None :
953
953
float_format = get_option ("display.float_format" )
954
954
if float_format is None :
0 commit comments