Skip to content

Commit 996f361

Browse files
Moisanjreback
authored andcommitted
DOC: Fix to_latex docstring. (#22516)
1 parent d8daddc commit 996f361

File tree

1 file changed

+80
-42
lines changed

1 file changed

+80
-42
lines changed

pandas/core/generic.py

+80-42
Original file line numberDiff line numberDiff line change
@@ -2595,69 +2595,107 @@ def to_xarray(self):
25952595
coords=coords,
25962596
)
25972597

2598-
_shared_docs['to_latex'] = r"""
2598+
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
2599+
index=True, na_rep='NaN', formatters=None, float_format=None,
2600+
sparsify=None, index_names=True, bold_rows=False,
2601+
column_format=None, longtable=None, escape=None,
2602+
encoding=None, decimal='.', multicolumn=None,
2603+
multicolumn_format=None, multirow=None):
2604+
r"""
2605+
Render an object to a LaTeX tabular environment table.
2606+
25992607
Render an object to a tabular environment table. You can splice
2600-
this into a LaTeX document. Requires \\usepackage{booktabs}.
2608+
this into a LaTeX document. Requires \usepackage{booktabs}.
26012609
26022610
.. versionchanged:: 0.20.2
26032611
Added to Series
26042612
2605-
`to_latex`-specific options:
2606-
2607-
bold_rows : boolean, default False
2608-
Make the row labels bold in the output
2609-
column_format : str, default None
2613+
Parameters
2614+
----------
2615+
buf : file descriptor or None
2616+
Buffer to write to. If None, the output is returned as a string.
2617+
columns : list of label, optional
2618+
The subset of columns to write. Writes all columns by default.
2619+
col_space : int, optional
2620+
The minimum width of each column.
2621+
header : bool or list of str, default True
2622+
Write out the column names. If a list of strings is given,
2623+
it is assumed to be aliases for the column names.
2624+
index : bool, default True
2625+
Write row names (index).
2626+
na_rep : str, default 'NaN'
2627+
Missing data representation.
2628+
formatters : list of functions or dict of {str: function}, optional
2629+
Formatter functions to apply to columns' elements by position or
2630+
name. The result of each function must be a unicode string.
2631+
List must be of length equal to the number of columns.
2632+
float_format : str, optional
2633+
Format string for floating point numbers.
2634+
sparsify : bool, optional
2635+
Set to False for a DataFrame with a hierarchical index to print
2636+
every multiindex key at each row. By default, the value will be
2637+
read from the config module.
2638+
index_names : bool, default True
2639+
Prints the names of the indexes.
2640+
bold_rows : bool, default False
2641+
Make the row labels bold in the output.
2642+
column_format : str, optional
26102643
The columns format as specified in `LaTeX table format
2611-
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl' for 3
2612-
columns
2613-
longtable : boolean, default will be read from the pandas config module
2614-
Default: False.
2615-
Use a longtable environment instead of tabular. Requires adding
2616-
a \\usepackage{longtable} to your LaTeX preamble.
2617-
escape : boolean, default will be read from the pandas config module
2618-
Default: True.
2619-
When set to False prevents from escaping latex special
2644+
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g. 'rcl' for 3
2645+
columns. By default, 'l' will be used for all columns except
2646+
columns of numbers, which default to 'r'.
2647+
longtable : bool, optional
2648+
By default, the value will be read from the pandas config
2649+
module. Use a longtable environment instead of tabular. Requires
2650+
adding a \usepackage{longtable} to your LaTeX preamble.
2651+
escape : bool, optional
2652+
By default, the value will be read from the pandas config
2653+
module. When set to False prevents from escaping latex special
26202654
characters in column names.
2621-
encoding : str, default None
2655+
encoding : str, optional
26222656
A string representing the encoding to use in the output file,
26232657
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
2624-
decimal : string, default '.'
2658+
decimal : str, default '.'
26252659
Character recognized as decimal separator, e.g. ',' in Europe.
2626-
26272660
.. versionadded:: 0.18.0
2628-
2629-
multicolumn : boolean, default True
2661+
multicolumn : bool, default True
26302662
Use \multicolumn to enhance MultiIndex columns.
26312663
The default will be read from the config module.
2632-
26332664
.. versionadded:: 0.20.0
2634-
26352665
multicolumn_format : str, default 'l'
26362666
The alignment for multicolumns, similar to `column_format`
26372667
The default will be read from the config module.
2638-
2668+
.. versionadded:: 0.20.0
2669+
multirow : bool, default False
2670+
Use \multirow to enhance MultiIndex rows. Requires adding a
2671+
\usepackage{multirow} to your LaTeX preamble. Will print
2672+
centered labels (instead of top-aligned) across the contained
2673+
rows, separating groups via clines. The default will be read
2674+
from the pandas config module.
26392675
.. versionadded:: 0.20.0
26402676
2641-
multirow : boolean, default False
2642-
Use \multirow to enhance MultiIndex rows.
2643-
Requires adding a \\usepackage{multirow} to your LaTeX preamble.
2644-
Will print centered labels (instead of top-aligned)
2645-
across the contained rows, separating groups via clines.
2646-
The default will be read from the pandas config module.
2677+
Returns
2678+
-------
2679+
str or None
2680+
If buf is None, returns the resulting LateX format as a
2681+
string. Otherwise returns None.
26472682
2648-
.. versionadded:: 0.20.0
2649-
"""
2683+
See Also
2684+
--------
2685+
DataFrame.to_string : Render a DataFrame to a console-friendly
2686+
tabular output.
2687+
DataFrame.to_html : Render a DataFrame as an HTML table.
26502688
2651-
@Substitution(header='Write out the column names. If a list of strings '
2652-
'is given, it is assumed to be aliases for the '
2653-
'column names.')
2654-
@Appender(_shared_docs['to_latex'] % _shared_doc_kwargs)
2655-
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
2656-
index=True, na_rep='NaN', formatters=None, float_format=None,
2657-
sparsify=None, index_names=True, bold_rows=False,
2658-
column_format=None, longtable=None, escape=None,
2659-
encoding=None, decimal='.', multicolumn=None,
2660-
multicolumn_format=None, multirow=None):
2689+
Examples
2690+
--------
2691+
>>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
2692+
... 'mask': ['red', 'purple'],
2693+
... 'weapon': ['sai', 'bo staff']})
2694+
>>> df.to_latex(index=False) # doctest: +NORMALIZE_WHITESPACE
2695+
'\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon
2696+
\\\\\n\\midrule\n Raphael & red & sai \\\\\n Donatello &
2697+
purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n'
2698+
"""
26612699
# Get defaults from the pandas config
26622700
if self.ndim == 1:
26632701
self = self.to_frame()

0 commit comments

Comments
 (0)