From 2d10549e8817063afa493a4187e6dc667f5e6805 Mon Sep 17 00:00:00 2001 From: Karel De Brabandere Date: Fri, 30 Jun 2017 22:34:11 +0200 Subject: [PATCH] BUG: fix to_latex bold_rows option (#16708) (cherry picked from commit 6b729ddd740b6d2efb739757180dbbbef9b092c7) --- doc/source/whatsnew/v0.20.3.txt | 2 +- pandas/core/generic.py | 4 ++-- pandas/io/formats/format.py | 6 +++++ pandas/tests/io/formats/test_to_latex.py | 30 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt index 0fac6367fd3a5..55da3812573c1 100644 --- a/doc/source/whatsnew/v0.20.3.txt +++ b/doc/source/whatsnew/v0.20.3.txt @@ -40,7 +40,6 @@ Bug Fixes - Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`) - Conversion ^^^^^^^^^^ @@ -57,6 +56,7 @@ I/O - 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`) - 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`) +- 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`) Plotting ^^^^^^^^ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1a1bbc37cd816..be90244119dc2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1508,7 +1508,7 @@ def to_xarray(self): `to_latex`-specific options: - bold_rows : boolean, default True + bold_rows : boolean, default False Make the row labels bold in the output column_format : str, default None The columns format as specified in `LaTeX table format @@ -1557,7 +1557,7 @@ def to_xarray(self): @Appender(_shared_docs['to_latex'] % _shared_doc_kwargs) def to_latex(self, buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, - sparsify=None, index_names=True, bold_rows=True, + sparsify=None, index_names=True, bold_rows=False, column_format=None, longtable=None, escape=None, encoding=None, decimal='.', multicolumn=None, multicolumn_format=None, multirow=None): diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 2b0c5899e91cb..78f92930e06a2 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -842,6 +842,7 @@ def __init__(self, formatter, column_format=None, longtable=False, multicolumn=False, multicolumn_format=None, multirow=False): self.fmt = formatter self.frame = self.fmt.frame + self.bold_rows = self.fmt.kwds.get('bold_rows', False) self.column_format = column_format self.longtable = longtable self.multicolumn = multicolumn @@ -940,6 +941,11 @@ def get_col_type(dtype): if x else '{}') for x in row] else: crow = [x if x else '{}' for x in row] + if self.bold_rows and self.fmt.index: + # bold row labels + crow = ['\\textbf{%s}' % x + if j < ilevels and x.strip() not in ['', '{}'] else x + for j, x in enumerate(crow)] if i < clevels and self.fmt.header and self.multicolumn: # sum up columns to multicolumns crow = self._format_multicolumn(crow, ilevels) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 4ee77abb32c26..aa86d1d9231fb 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -506,3 +506,33 @@ def test_to_latex_series(self): \end{tabular} """ assert withindex_result == withindex_expected + + def test_to_latex_bold_rows(self): + # GH 16707 + df = pd.DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) + observed = df.to_latex(bold_rows=True) + expected = r"""\begin{tabular}{lrl} +\toprule +{} & a & b \\ +\midrule +\textbf{0} & 1 & b1 \\ +\textbf{1} & 2 & b2 \\ +\bottomrule +\end{tabular} +""" + assert observed == expected + + def test_to_latex_no_bold_rows(self): + # GH 16707 + df = pd.DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) + observed = df.to_latex(bold_rows=False) + expected = r"""\begin{tabular}{lrl} +\toprule +{} & a & b \\ +\midrule +0 & 1 & b1 \\ +1 & 2 & b2 \\ +\bottomrule +\end{tabular} +""" + assert observed == expected