1
1
from StringIO import StringIO
2
- from pandas .core .common import adjoin , _pfixed
2
+ from pandas .core .common import adjoin , is_numeric_dtype
3
3
from pandas .core .index import MultiIndex , _ensure_index
4
4
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 """
5
33
6
34
class DataFrameFormatter (object ):
7
35
"""
8
36
Render a DataFrame
9
37
10
38
self.to_string() : console-friendly tabular output
11
- self.to_html() : html table
39
+ self.to_html() : html table
40
+
12
41
"""
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 ):
16
42
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 ):
17
48
self .frame = frame
18
49
self .buf = buf if buf is not None else StringIO ()
19
50
self .show_index_names = index_names
@@ -22,6 +53,8 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
22
53
self .formatters = formatters
23
54
self .na_rep = na_rep
24
55
self .col_space = col_space
56
+ self .header = header
57
+ self .index = index
25
58
26
59
if columns is not None :
27
60
self .columns = _ensure_index (columns )
@@ -47,10 +80,16 @@ def to_string(self):
47
80
str_index = self ._get_formatted_index ()
48
81
str_columns = self ._get_formatted_column_labels ()
49
82
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 ]
52
88
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 ))
54
93
55
94
for s in to_write :
56
95
if isinstance (s , unicode ):
@@ -114,17 +153,21 @@ def _column_header():
114
153
write (buf , '</tbody>' , indent + indent_delta )
115
154
else :
116
155
indent += indent_delta
117
- write (buf , '<thead>' , indent )
118
- row = []
119
156
120
157
# 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
+
128
171
write (buf , '<tbody>' , indent )
129
172
130
173
# write values
@@ -148,14 +191,9 @@ def _get_column_formatter(self):
148
191
149
192
col_space = self .col_space
150
193
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 )
159
197
160
198
formatters = {} if self .formatters is None else self .formatters
161
199
@@ -171,16 +209,24 @@ def _format_col(col, i=None):
171
209
def _get_formatted_column_labels (self ):
172
210
from pandas .core .index import _sparsify
173
211
212
+ formatters = self .formatters
213
+ if formatters is None :
214
+ formatters = {}
215
+
174
216
if isinstance (self .columns , MultiIndex ):
175
217
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 ]
177
221
for x in zip (* fmt_columns )])
178
222
if self .sparsify :
179
223
str_columns = _sparsify (str_columns )
180
224
181
225
str_columns = [list (x ) for x in zip (* str_columns )]
182
226
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 ()]
184
230
185
231
if self .show_index_names and self .has_index_names :
186
232
for x in str_columns :
@@ -201,7 +247,7 @@ def _get_formatted_index(self):
201
247
columns = self .frame .columns
202
248
203
249
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 )
205
251
206
252
if isinstance (index , MultiIndex ):
207
253
fmt_index = index .format (sparsify = self .sparsify , adjoin = False ,
@@ -213,11 +259,14 @@ def _get_formatted_index(self):
213
259
214
260
# empty space for columns
215
261
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 ()]
217
263
else :
218
264
col_header = ['' ] * columns .nlevels
219
265
220
- return col_header + adjoined
266
+ if self .header :
267
+ return col_header + adjoined
268
+ else :
269
+ return adjoined
221
270
222
271
def _get_column_name_list (self ):
223
272
names = []
@@ -229,7 +278,6 @@ def _get_column_name_list(self):
229
278
names .append ('' if columns .name is None else columns .name )
230
279
return names
231
280
232
-
233
281
def single_column_table (column ):
234
282
table = '<table><tbody>'
235
283
for i in column :
0 commit comments