40
40
string representation of NAN to use, default 'NaN'
41
41
formatters : list or dict of one-parameter functions, optional
42
42
formatter functions to apply to columns' elements by position or name,
43
- default None, if the result is a string , it must be a unicode string.
43
+ default None, if the result is a string , it must be a unicode
44
+ string. List must be of length equal to the number of columns.
44
45
float_format : one-parameter function, optional
45
46
formatter function to apply to columns' elements if they are floats
46
47
default None
@@ -170,8 +171,22 @@ def _strlen(x):
170
171
171
172
return _strlen
172
173
174
+ class TableFormatter (object ):
173
175
174
- class DataFrameFormatter (object ):
176
+
177
+ def _get_formatter (self , i ):
178
+ if isinstance (self .formatters , (list , tuple )):
179
+ if com .is_integer (i ):
180
+ return self .formatters [i ]
181
+ else :
182
+ return None
183
+ else :
184
+ if com .is_integer (i ) and i not in self .columns :
185
+ i = self .columns [i ]
186
+ return self .formatters .get (i , None )
187
+
188
+
189
+ class DataFrameFormatter (TableFormatter ):
175
190
"""
176
191
Render a DataFrame
177
192
@@ -361,8 +376,7 @@ def to_latex(self, force_unicode=None, column_format=None):
361
376
self .buf .write ('\\ end{tabular}\n ' )
362
377
363
378
def _format_col (self , i ):
364
- col = self .columns [i ]
365
- formatter = self .formatters .get (col )
379
+ formatter = self ._get_formatter (i )
366
380
return format_array (self .frame .icol (i ).values , formatter ,
367
381
float_format = self .float_format ,
368
382
na_rep = self .na_rep ,
@@ -399,9 +413,10 @@ def is_numeric_dtype(dtype):
399
413
dtypes = self .frame .dtypes
400
414
need_leadsp = dict (zip (fmt_columns , map (is_numeric_dtype , dtypes )))
401
415
str_columns = [[' ' + x
402
- if col not in self .formatters and need_leadsp [x ]
416
+ if not self ._get_formatter ( i ) and need_leadsp [x ]
403
417
else x ]
404
- for col , x in zip (self .columns , fmt_columns )]
418
+ for i , (col , x ) in
419
+ enumerate (zip (self .columns , fmt_columns ))]
405
420
406
421
if self .show_index_names and self .has_index_names :
407
422
for x in str_columns :
@@ -425,7 +440,8 @@ def _get_formatted_index(self):
425
440
show_index_names = self .show_index_names and self .has_index_names
426
441
show_col_names = (self .show_index_names and self .has_column_names )
427
442
428
- fmt = self .formatters .get ('__index__' , None )
443
+ fmt = self ._get_formatter ('__index__' )
444
+
429
445
if isinstance (index , MultiIndex ):
430
446
fmt_index = index .format (sparsify = self .sparsify , adjoin = False ,
431
447
names = show_index_names ,
@@ -457,7 +473,7 @@ def _get_column_name_list(self):
457
473
return names
458
474
459
475
460
- class HTMLFormatter (object ):
476
+ class HTMLFormatter (TableFormatter ):
461
477
462
478
indent_delta = 2
463
479
@@ -656,9 +672,9 @@ def _write_body(self, indent):
656
672
def _write_regular_rows (self , fmt_values , indent ):
657
673
ncols = len (self .columns )
658
674
659
- if '__index__' in self .fmt .formatters :
660
- f = self . fmt . formatters [ '__index__' ]
661
- index_values = self .frame .index .map (f )
675
+ fmt = self .fmt ._get_formatter ( '__index__' )
676
+ if fmt is not None :
677
+ index_values = self .frame .index .map (fmt )
662
678
else :
663
679
index_values = self .frame .index .format ()
664
680
0 commit comments