diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 3a539199acd6f..00f3878766e31 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -355,6 +355,9 @@ I/O - Improved performance in :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` when converting columns that have missing values (:issue:`25772`) - Bug in :func:`read_hdf` not properly closing store after a ``KeyError`` is raised (:issue:`25766`) - Bug in ``read_csv`` which would not raise ``ValueError`` if a column index in ``usecols`` was out of bounds (:issue:`25623`) +- Bug in :meth:`DataFrame.to_latex` that would ignore ``na_rep`` if the ``formatters`` argument was used (:issue:`9046`) +- + Plotting ^^^^^^^^ diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index b658c8a53dc8b..d4dfe46dcb3d7 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1063,7 +1063,10 @@ def get_result_as_array(self): """ if self.formatter is not None: - return np.array([self.formatter(x) for x in self.values]) + out = np.array([self.formatter(x) for x in self.values]) + mask = isna(self.values) + out[mask] = self.na_rep + return out if self.fixed_width: threshold = get_option("display.chop_threshold") @@ -1142,7 +1145,8 @@ def format_values_with(float_format): def _format_strings(self): # shortcut if self.formatter is not None: - return [self.formatter(x) for x in self.values] + return [self.formatter(x) if not isna(x) else self.na_rep + for x in self.values] return list(self.get_result_as_array()) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index a2b65dab9a0a2..903280b11545b 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -741,5 +741,22 @@ def test_to_latex_multindex_header(self): 0 & 1 & 2 & 3 \\ \bottomrule \end{tabular} +""" + assert observed == expected + + def test_to_latex_na_rep_formatters(self): + # GH 9046 + df = pd.DataFrame({'a': [0, 1, 2], 'b': [0.1, None, 0.2]}) + observed = df.to_latex( + formatters={'b': '{:0.4f}'.format}, na_rep='-') + expected = r"""\begin{tabular}{lrr} +\toprule +{} & a & b \\ +\midrule +0 & 0 & 0.1000 \\ +1 & 1 & - \\ +2 & 2 & 0.2000 \\ +\bottomrule +\end{tabular} """ assert observed == expected