Skip to content

Commit 6b729dd

Browse files
kdebrabjreback
authored andcommitted
BUG: fix to_latex bold_rows option (#16708)
1 parent 06fc667 commit 6b729dd

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Bug Fixes
4141
- Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`)
4242

4343

44-
4544
Conversion
4645
^^^^^^^^^^
4746

@@ -59,6 +58,7 @@ I/O
5958

6059
- Bug in :func:`read_csv` in which files weren't opened as binary files by the C engine on Windows, causing EOF characters mid-field, which would fail (:issue:`16039`, :issue:`16559`, :issue:`16675`)
6160
- Bug in :func:`read_hdf` in which reading a ``Series`` saved to an HDF file in 'fixed' format fails when an explicit ``mode='r'`` argument is supplied (:issue:`16583`)
61+
- Bug in :func:`DataFrame.to_latex` where ``bold_rows`` was wrongly specified to be ``True`` by default, whereas in reality row labels remained non-bold whatever parameter provided. (:issue:`16707`)
6262

6363
Plotting
6464
^^^^^^^^

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ def to_xarray(self):
15381538
15391539
`to_latex`-specific options:
15401540
1541-
bold_rows : boolean, default True
1541+
bold_rows : boolean, default False
15421542
Make the row labels bold in the output
15431543
column_format : str, default None
15441544
The columns format as specified in `LaTeX table format
@@ -1587,7 +1587,7 @@ def to_xarray(self):
15871587
@Appender(_shared_docs['to_latex'] % _shared_doc_kwargs)
15881588
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
15891589
index=True, na_rep='NaN', formatters=None, float_format=None,
1590-
sparsify=None, index_names=True, bold_rows=True,
1590+
sparsify=None, index_names=True, bold_rows=False,
15911591
column_format=None, longtable=None, escape=None,
15921592
encoding=None, decimal='.', multicolumn=None,
15931593
multicolumn_format=None, multirow=None):

pandas/io/formats/format.py

+6
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ def __init__(self, formatter, column_format=None, longtable=False,
845845
multicolumn=False, multicolumn_format=None, multirow=False):
846846
self.fmt = formatter
847847
self.frame = self.fmt.frame
848+
self.bold_rows = self.fmt.kwds.get('bold_rows', False)
848849
self.column_format = column_format
849850
self.longtable = longtable
850851
self.multicolumn = multicolumn
@@ -943,6 +944,11 @@ def get_col_type(dtype):
943944
if x else '{}') for x in row]
944945
else:
945946
crow = [x if x else '{}' for x in row]
947+
if self.bold_rows and self.fmt.index:
948+
# bold row labels
949+
crow = ['\\textbf{%s}' % x
950+
if j < ilevels and x.strip() not in ['', '{}'] else x
951+
for j, x in enumerate(crow)]
946952
if i < clevels and self.fmt.header and self.multicolumn:
947953
# sum up columns to multicolumns
948954
crow = self._format_multicolumn(crow, ilevels)

pandas/tests/io/formats/test_to_latex.py

+30
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,33 @@ def test_to_latex_series(self):
506506
\end{tabular}
507507
"""
508508
assert withindex_result == withindex_expected
509+
510+
def test_to_latex_bold_rows(self):
511+
# GH 16707
512+
df = pd.DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
513+
observed = df.to_latex(bold_rows=True)
514+
expected = r"""\begin{tabular}{lrl}
515+
\toprule
516+
{} & a & b \\
517+
\midrule
518+
\textbf{0} & 1 & b1 \\
519+
\textbf{1} & 2 & b2 \\
520+
\bottomrule
521+
\end{tabular}
522+
"""
523+
assert observed == expected
524+
525+
def test_to_latex_no_bold_rows(self):
526+
# GH 16707
527+
df = pd.DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
528+
observed = df.to_latex(bold_rows=False)
529+
expected = r"""\begin{tabular}{lrl}
530+
\toprule
531+
{} & a & b \\
532+
\midrule
533+
0 & 1 & b1 \\
534+
1 & 2 & b2 \\
535+
\bottomrule
536+
\end{tabular}
537+
"""
538+
assert observed == expected

0 commit comments

Comments
 (0)