Skip to content

Commit b75c4d3

Browse files
committed
ENH: add bold_rows option to DataFrame.to_html, GH pandas-dev#586
1 parent 329e59e commit b75c4d3

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

RELEASE.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ pandas 0.7.0
5252
- Add attribute-based item access to ``Panel`` and add IPython completion (PR
5353
#554)
5454
- Add ``logy`` option to ``Series.plot`` for log-scaling on the Y axis
55-
- Add ``index`` and ``header`` options to ``DataFrame.to_string`` (GH #570)
55+
- Add ``index``, ``header``, and ``justify`` options to
56+
``DataFrame.to_string`` (GH #570, GH #571)
5657
- Can pass multiple DataFrames to ``DataFrame.join`` to join on index (GH #115)
5758
- Can pass multiple Panels to ``Panel.join`` (GH #115)
5859
- Can pass multiple DataFrames to `DataFrame.append` to concatenate (stack)
@@ -105,6 +106,7 @@ pandas 0.7.0
105106
DataFrame objects
106107
- Made ``Index._get_duplicates`` a public method by removing the underscore
107108
- Prettier printing of floats, and column spacing fix (GH #395, GH #571)
109+
- Add ``bold_rows`` option to DataFrame.to_html (GH #586)
108110

109111
**Bug fixes**
110112

pandas/core/format.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DataFrameFormatter(object):
4949
def __init__(self, frame, buf=None, columns=None, col_space=None,
5050
header=True, index=True, na_rep='NaN', formatters=None,
5151
justify='left', float_format=None, sparsify=True,
52-
index_names=True):
52+
index_names=True, **kwds):
5353
self.frame = frame
5454
self.buf = buf if buf is not None else StringIO()
5555
self.show_index_names = index_names
@@ -62,6 +62,8 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
6262
self.index = index
6363
self.justify = justify
6464

65+
self.kwds = kwds
66+
6567
if columns is not None:
6668
self.columns = _ensure_index(columns)
6769
else:
@@ -217,13 +219,22 @@ def _column_header():
217219

218220
write(buf, '<tbody>', indent)
219221

222+
_bold_row = self.kwds.get('bold_rows', False)
223+
def _maybe_bold_row(x):
224+
temp = '<strong>%s</strong>'
225+
if _bold_row:
226+
return ([temp % y for y in x] if isinstance(x, tuple)
227+
else temp % x)
228+
else:
229+
return x
230+
220231
# write values
221232
for i in range(len(frame)):
222233
row = []
223234
if isinstance(frame.index, MultiIndex):
224-
row.extend(frame.index[i])
235+
row.extend(_maybe_bold_row(frame.index[i]))
225236
else:
226-
row.append(frame.index[i])
237+
row.append(_maybe_bold_row(frame.index[i]))
227238
for column in frame.columns:
228239
row.append(self._format_col(column, i))
229240
write_tr(buf, row, indent, indent_delta)

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,13 @@ def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
955955
@Appender(docstring_to_string, indents=1)
956956
def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
957957
header=True, index=True, na_rep='NaN', formatters=None,
958-
float_format=None, sparsify=True, index_names=True):
958+
float_format=None, sparsify=True, index_names=True,
959+
bold_rows=True):
959960
"""
961+
to_html-specific options
962+
bold_rows : boolean, default True
963+
Make the row labels bold in the output
964+
960965
Render a DataFrame to an html table.
961966
"""
962967

@@ -971,6 +976,7 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
971976
header=header, index=index,
972977
formatters=formatters,
973978
float_format=float_format,
979+
bold_rows=bold_rows,
974980
sparsify=sparsify,
975981
index_names=index_names)
976982
formatter.to_html()

pandas/tests/test_frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -4151,4 +4151,3 @@ def test_stale_cached_series_bug_473(self):
41514151
# exit=False)
41524152
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
41534153
exit=False)
4154-

0 commit comments

Comments
 (0)