11from StringIO import StringIO
2- from pandas .core .common import adjoin , _pfixed
2+ from pandas .core .common import adjoin , is_numeric_dtype
33from pandas .core .index import MultiIndex , _ensure_index
44
5+ docstring_to_string = """
6+ Parameters
7+ ----------
8+ frame : DataFrame
9+ object to render
10+ buf : StringIO-like, optional
11+ buffer to write to
12+ columns : sequence, optional
13+ the subset of columns to write; default None writes all columns
14+ col_space : int, optional
15+ the width of each columns
16+ header : bool, optional
17+ whether to print column labels, default True
18+ index : bool, optional
19+ whether to print index (row) labels, default True
20+ na_rep : string, optional
21+ string representation of NAN to use, default 'NaN'
22+ formatters : list or dict of one-parameter functions, optional
23+ formatter functions to apply to columns' elements by position or name,
24+ default None
25+ float_format : one-parameter function, optional
26+ formatter function to apply to columns' elements if they are floats
27+ default None
28+ sparsify : bool, optional
29+ Set to False for a DataFrame with a hierarchical index to print every
30+ multiindex key at each row, default True
31+ index_names : bool, optional
32+ Prints the names of the indexes, default True """
533
634class DataFrameFormatter (object ):
735 """
836 Render a DataFrame
937
1038 self.to_string() : console-friendly tabular output
11- self.to_html() : html table
39+ self.to_html() : html table
40+
1241 """
13- def __init__ (self , frame , buf = None , columns = None , col_space = None ,
14- na_rep = 'NaN' , formatters = None , float_format = None ,
15- sparsify = True , index_names = True ):
1642
43+ __doc__ += docstring_to_string
44+
45+ def __init__ (self , frame , buf = None , columns = None , col_space = None ,
46+ header = True , index = True , na_rep = 'NaN' , formatters = None ,
47+ float_format = None , sparsify = True , index_names = True ):
1748 self .frame = frame
1849 self .buf = buf if buf is not None else StringIO ()
1950 self .show_index_names = index_names
@@ -22,6 +53,8 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
2253 self .formatters = formatters
2354 self .na_rep = na_rep
2455 self .col_space = col_space
56+ self .header = header
57+ self .index = index
2558
2659 if columns is not None :
2760 self .columns = _ensure_index (columns )
@@ -47,10 +80,16 @@ def to_string(self):
4780 str_index = self ._get_formatted_index ()
4881 str_columns = self ._get_formatted_column_labels ()
4982
50- stringified = [str_columns [i ] + format_col (c )
51- for i , c in enumerate (self .columns )]
83+ if self .header :
84+ stringified = [str_columns [i ] + format_col (c )
85+ for i , c in enumerate (self .columns )]
86+ else :
87+ stringified = [format_col (c ) for c in self .columns ]
5288
53- to_write .append (adjoin (1 , str_index , * stringified ))
89+ if self .index :
90+ to_write .append (adjoin (1 , str_index , * stringified ))
91+ else :
92+ to_write .append (adjoin (1 , * stringified ))
5493
5594 for s in to_write :
5695 if isinstance (s , unicode ):
@@ -114,17 +153,21 @@ def _column_header():
114153 write (buf , '</tbody>' , indent + indent_delta )
115154 else :
116155 indent += indent_delta
117- write (buf , '<thead>' , indent )
118- row = []
119156
120157 # header row
121- col_row = _column_header ()
122- indent += indent_delta
123- write_tr (buf , col_row , indent , indent_delta , header = True )
124- if self .has_index_names :
125- row = frame .index .names + ['' ] * len (frame .columns )
126- write_tr (buf , row , indent , indent_delta , header = True )
127- write (buf , '</thead>' , indent )
158+ if self .header :
159+ write (buf , '<thead>' , indent )
160+ row = []
161+
162+ col_row = _column_header ()
163+ indent += indent_delta
164+ write_tr (buf , col_row , indent , indent_delta , header = True )
165+ if self .has_index_names :
166+ row = frame .index .names + ['' ] * len (frame .columns )
167+ write_tr (buf , row , indent , indent_delta , header = True )
168+
169+ write (buf , '</thead>' , indent )
170+
128171 write (buf , '<tbody>' , indent )
129172
130173 # write values
@@ -148,14 +191,9 @@ def _get_column_formatter(self):
148191
149192 col_space = self .col_space
150193
151- if col_space is None :
152- def _myformat (v ):
153- return _format (v , na_rep = self .na_rep ,
154- float_format = self .float_format )
155- else :
156- def _myformat (v ):
157- return _pfixed (v , col_space , na_rep = self .na_rep ,
158- float_format = self .float_format )
194+ def _myformat (v ):
195+ return _format (v , space = col_space , na_rep = self .na_rep ,
196+ float_format = self .float_format )
159197
160198 formatters = {} if self .formatters is None else self .formatters
161199
@@ -171,16 +209,24 @@ def _format_col(col, i=None):
171209 def _get_formatted_column_labels (self ):
172210 from pandas .core .index import _sparsify
173211
212+ formatters = self .formatters
213+ if formatters is None :
214+ formatters = {}
215+
174216 if isinstance (self .columns , MultiIndex ):
175217 fmt_columns = self .columns .format (sparsify = False , adjoin = False )
176- str_columns = zip (* [[' %s' % y for y in x ]
218+ str_columns = zip (* [[' %s' % y if y not in formatters and is_numeric_dtype (self .frame [x ])
219+ else str (y )
220+ for y in x ]
177221 for x in zip (* fmt_columns )])
178222 if self .sparsify :
179223 str_columns = _sparsify (str_columns )
180224
181225 str_columns = [list (x ) for x in zip (* str_columns )]
182226 else :
183- str_columns = [[' %s' % x ] for x in self .columns .format ()]
227+ str_columns = [[' %s' % x if x not in formatters and is_numeric_dtype (self .frame [x ])
228+ else str (x )]
229+ for x in self .columns .format ()]
184230
185231 if self .show_index_names and self .has_index_names :
186232 for x in str_columns :
@@ -201,7 +247,7 @@ def _get_formatted_index(self):
201247 columns = self .frame .columns
202248
203249 show_index_names = self .show_index_names and self .has_index_names
204- show_col_names = self .show_index_names and self .has_column_names
250+ show_col_names = ( self .show_index_names and self .has_column_names )
205251
206252 if isinstance (index , MultiIndex ):
207253 fmt_index = index .format (sparsify = self .sparsify , adjoin = False ,
@@ -213,11 +259,14 @@ def _get_formatted_index(self):
213259
214260 # empty space for columns
215261 if show_col_names :
216- col_header = [' %s' % x for x in self ._get_column_name_list ()]
262+ col_header = ['%s' % x for x in self ._get_column_name_list ()]
217263 else :
218264 col_header = ['' ] * columns .nlevels
219265
220- return col_header + adjoined
266+ if self .header :
267+ return col_header + adjoined
268+ else :
269+ return adjoined
221270
222271 def _get_column_name_list (self ):
223272 names = []
@@ -229,7 +278,6 @@ def _get_column_name_list(self):
229278 names .append ('' if columns .name is None else columns .name )
230279 return names
231280
232-
233281def single_column_table (column ):
234282 table = '<table><tbody>'
235283 for i in column :
0 commit comments