Skip to content

Commit 8217493

Browse files
kdebrabTomAugspurger
authored andcommitted
BUG: fix to_latex bold_rows option (#16708)
(cherry picked from commit 6b729dd)
1 parent 68e041f commit 8217493

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
@@ -40,7 +40,6 @@ Bug Fixes
4040
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
4141

4242

43-
4443
Conversion
4544
^^^^^^^^^^
4645

@@ -57,6 +56,7 @@ I/O
5756

5857
- 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`)
5958
- 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`)
59+
- 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`)
6060

6161
Plotting
6262
^^^^^^^^

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ def to_xarray(self):
15081508
15091509
`to_latex`-specific options:
15101510
1511-
bold_rows : boolean, default True
1511+
bold_rows : boolean, default False
15121512
Make the row labels bold in the output
15131513
column_format : str, default None
15141514
The columns format as specified in `LaTeX table format
@@ -1557,7 +1557,7 @@ def to_xarray(self):
15571557
@Appender(_shared_docs['to_latex'] % _shared_doc_kwargs)
15581558
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
15591559
index=True, na_rep='NaN', formatters=None, float_format=None,
1560-
sparsify=None, index_names=True, bold_rows=True,
1560+
sparsify=None, index_names=True, bold_rows=False,
15611561
column_format=None, longtable=None, escape=None,
15621562
encoding=None, decimal='.', multicolumn=None,
15631563
multicolumn_format=None, multirow=None):

pandas/io/formats/format.py

+6
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ def __init__(self, formatter, column_format=None, longtable=False,
842842
multicolumn=False, multicolumn_format=None, multirow=False):
843843
self.fmt = formatter
844844
self.frame = self.fmt.frame
845+
self.bold_rows = self.fmt.kwds.get('bold_rows', False)
845846
self.column_format = column_format
846847
self.longtable = longtable
847848
self.multicolumn = multicolumn
@@ -940,6 +941,11 @@ def get_col_type(dtype):
940941
if x else '{}') for x in row]
941942
else:
942943
crow = [x if x else '{}' for x in row]
944+
if self.bold_rows and self.fmt.index:
945+
# bold row labels
946+
crow = ['\\textbf{%s}' % x
947+
if j < ilevels and x.strip() not in ['', '{}'] else x
948+
for j, x in enumerate(crow)]
943949
if i < clevels and self.fmt.header and self.multicolumn:
944950
# sum up columns to multicolumns
945951
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)