@@ -3263,6 +3263,9 @@ def to_latex(
32633263 .. versionchanged:: 1.2.0
32643264 Added position argument, changed meaning of caption argument.
32653265
3266+ .. versionchanged:: 1.5.0
3267+ Refactored to use the Styler implementation via jinja2 templating.
3268+
32663269 Parameters
32673270 ----------
32683271 buf : str, Path or StringIO-like, optional, default None
@@ -3347,26 +3350,75 @@ def to_latex(
33473350 ``\begin{{}}`` in the output.
33483351
33493352 .. versionadded:: 1.2.0
3350- {returns}
3353+
3354+ Returns
3355+ -------
3356+ str or None
3357+ If buf is None, returns the result as a string. Otherwise returns None.
3358+
33513359 See Also
33523360 --------
33533361 Styler.to_latex : Render a DataFrame to LaTeX with conditional formatting.
33543362 DataFrame.to_string : Render a DataFrame to a console-friendly
33553363 tabular output.
33563364 DataFrame.to_html : Render a DataFrame as an HTML table.
33573365
3366+ Notes
3367+ -----
3368+
3369+ .. note::
3370+ As of v1.5.0 this method has changed to use the Styler implementation of
3371+ ``to_latex`` and no longer uses the DataFrameRenderer. It is advised that
3372+ users switch to using Styler, since this implementation is more frequently
3373+ updated and contains much more flexibility with the output. The following
3374+ examples indicate how this method now replicates the Styler implementation
3375+ for its legacy arguments.
3376+
3377+ .. code-block:: python
3378+
3379+ styler = df.style
3380+
3381+ Styler methods are designed to be chained, so we can build complex combinations
3382+ of displays. To hide ``index`` and ``columns`` headers we use,
3383+
3384+ .. code-block:: python
3385+
3386+ styler.hide(axis="index").hide(axis="columns")
3387+
3388+ To use ``formatters``, ``na_rep``, ``decimal`` and ``float_format``,
3389+ ``escape`` we use,
3390+
3391+ .. code-block:: python
3392+
3393+ styler.format(
3394+ formatter={"name": str.upper}, na_rep="-", precision=1, escape="latex"
3395+ )
3396+
3397+ To control other aspects we use the ``Styler.to_latex`` arguments such as,
3398+
3399+ .. code-block:: python
3400+
3401+ styler.to_latex(
3402+ column_format="lrr", caption="my table", environment="longtable"
3403+ )
3404+
33583405 Examples
33593406 --------
3407+ Convert a general DataFrame to LaTeX with formatting:
3408+
33603409 >>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'],
3361- ... mask=['red', 'purple'],
3362- ... weapon=['sai', 'bo staff']))
3363- >>> print(df.to_latex(index=False)) # doctest: +SKIP
3364- \begin{{tabular}}{{lll}}
3365- \toprule
3366- name & mask & weapon \\
3367- \midrule
3368- Raphael & red & sai \\
3369- Donatello & purple & bo staff \\
3410+ ... age=[26, 45],
3411+ ... height=[181.23, 177.65]))
3412+ >>> print(df.to_latex(index=False,
3413+ ... formatters={"name": str.upper},
3414+ ... float_format="{:.1f}".format,
3415+ ... ) # doctest: +SKIP
3416+ \begin{{tabular}}{{lrr}}
3417+ \toprule
3418+ name & age & height \\
3419+ \\midrule
3420+ RAPHAEL & 26 & 181.2 \\
3421+ DONATELLO & 45 & 177.7 \\
33703422 \bottomrule
33713423 \end{{tabular}}
33723424 """
@@ -3430,10 +3482,9 @@ def _wrap(x, alt_format_):
34303482 if column_formatter is not None :
34313483 column_format_ .update ({"formatter" : column_formatter })
34323484
3433- formatters = {
3434- k : functools .partial (_wrap , alt_format_ = v )
3435- for k , v in formatters .items ()
3436- }
3485+ float_columns = self .select_dtypes (include = "float" ).columns
3486+ for col in [c for c in float_columns if c not in formatters .keys ()]:
3487+ formatters .update ({col : float_format_ })
34373488 elif formatters is None and float_format is not None :
34383489 formatters = functools .partial (_wrap , alt_format_ = lambda v : v )
34393490 else :
0 commit comments