@@ -3263,6 +3263,9 @@ def to_latex(
3263
3263
.. versionchanged:: 1.2.0
3264
3264
Added position argument, changed meaning of caption argument.
3265
3265
3266
+ .. versionchanged:: 1.5.0
3267
+ Refactored to use the Styler implementation via jinja2 templating.
3268
+
3266
3269
Parameters
3267
3270
----------
3268
3271
buf : str, Path or StringIO-like, optional, default None
@@ -3347,26 +3350,75 @@ def to_latex(
3347
3350
``\begin{{}}`` in the output.
3348
3351
3349
3352
.. 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
+
3351
3359
See Also
3352
3360
--------
3353
3361
Styler.to_latex : Render a DataFrame to LaTeX with conditional formatting.
3354
3362
DataFrame.to_string : Render a DataFrame to a console-friendly
3355
3363
tabular output.
3356
3364
DataFrame.to_html : Render a DataFrame as an HTML table.
3357
3365
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
+
3358
3405
Examples
3359
3406
--------
3407
+ Convert a general DataFrame to LaTeX with formatting:
3408
+
3360
3409
>>> 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 \\
3370
3422
\bottomrule
3371
3423
\end{{tabular}}
3372
3424
"""
@@ -3430,10 +3482,9 @@ def _wrap(x, alt_format_):
3430
3482
if column_formatter is not None :
3431
3483
column_format_ .update ({"formatter" : column_formatter })
3432
3484
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_ })
3437
3488
elif formatters is None and float_format is not None :
3438
3489
formatters = functools .partial (_wrap , alt_format_ = lambda v : v )
3439
3490
else :
0 commit comments