Skip to content

implemented additionally functionality of formatters_col in to_latex #32666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
13 changes: 13 additions & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.. _whatsnew_120:

What's new in 1.2.0 (??)
Expand Down Expand Up @@ -435,6 +436,18 @@ ExtensionArray
- Fixed bug where ``astype()`` with equal dtype and ``copy=False`` would return a new object (:issue:`284881`)
-

ToLatex
^^^^^^^

- Can now substitute simple fstrings in place of callable functions in to_latex

.. ipython:: python

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9.001]]
df = pd.DataFrame(data, columns=["a", "b", "c"],
index=["foo", "bar", "foobar"])
result = df.to_latex(formatters=["d", ".2f", lambda x: f"{x:.3f}"])


Other
^^^^^
Expand Down
18 changes: 17 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,7 @@ def to_latex(
Write row names (index).
na_rep : str, default 'NaN'
Missing data representation.
formatters : list of functions or dict of {{str: function}}, optional
formatters : list of functions/str or dict of {{str: function}}, optional
Formatter functions to apply to columns' elements by position or
name. The result of each function must be a unicode string.
List must be of length equal to the number of columns.
Expand Down Expand Up @@ -3112,6 +3112,22 @@ def to_latex(
if multirow is None:
multirow = config.get_option("display.latex.multirow")

if is_list_like(formatters) and not isinstance(formatters, dict):
formatter_elems_type = all(
isinstance(elem, str) or callable(elem) for elem in formatters
)
if formatter_elems_type:
formatters = [
(lambda style: lambda x: "{0:{1}}".format(x, style))(style)
if isinstance(style, str)
else style
for style in formatters
]
else:
raise ValueError(
"Formatters elements should be f-strings or callable functions"
)

formatter = DataFrameFormatter(
self,
columns=columns,
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ def test_to_latex_midrule_location(self):
)
assert result == expected

def test_to_latex_with_float_format_list(self):
# GH: 26278
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9.001]]
df = DataFrame(data, columns=["a", "b", "c"], index=["foo", "bar", "foobar"])

result = df.to_latex(formatters=["d", ".2f", lambda x: f"{x:.3f}"])
expected = r"""\begin{tabular}{lrrr}
\toprule
{} & a & b & c \\
\midrule
foo & 1 & 2.00 & 3.000 \\
bar & 4 & 5.00 & 6.000 \\
foobar & 7 & 8.00 & 9.001 \\
\bottomrule
\end{tabular}
"""
assert result == expected


class TestToLatexLongtable:
def test_to_latex_empty_longtable(self):
Expand Down