Skip to content

DOC: Fix to_latex docstring. #22516

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

Merged
merged 4 commits into from
Sep 8, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 70 additions & 37 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2521,69 +2521,102 @@ def to_xarray(self):
coords=coords,
)

_shared_docs['to_latex'] = r"""
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
index=True, na_rep='NaN', formatters=None, float_format=None,
sparsify=None, index_names=True, bold_rows=False,
column_format=None, longtable=None, escape=None,
encoding=None, decimal='.', multicolumn=None,
multicolumn_format=None, multirow=None):
r"""
Render an object to a tabular environment table. You can splice
this into a LaTeX document. Requires \\usepackage{booktabs}.
this into a LaTeX document. Requires \usepackage{booktabs}.

.. versionchanged:: 0.20.2
Added to Series

`to_latex`-specific options:

Parameters
----------
buf : StringIO-like, optional
Buffer to write to.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More than StringIO-like I'd say this is a file descriptor. I guess as other methods, if None, the output is returned as a string. I think in this cases we usually say file descriptor or None, instead of optional, but in either case, we want to explain that if None it returns the string.

columns : sequence, optional, default None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ened for default None when it's optional. I think we agreed on using label when talking about the objects in the indices, so it could make sense to have the type as list of label.

The subset of columns to write. Writes all columns by default.
col_space : int, optional
The minimum width of each column.
header : bool or list of str, default True
Write out the column names. If a list of strings is given,
it is assumed to be aliases for the column names.
index : bool, default True
Write row names (index).
na_rep : str, default 'NaN'
Missing data representation.
formatters : list or dict of one-param. functions, optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is a bit confusing. Don't know what it's expected, would it be list of function or dict of {str: function}?

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.
float_format : str, default None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional instead of default None would make more sense in this case to me. It's not always clear, but in general we use default None when the None value is used as None. When None means the feature is not used, it's optional.

Format string for floating point numbers.
sparsify : bool, optional, default None
Set to False for a DataFrame with a hierarchical index to print
every multiindex key at each row.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if None and False are the same here? if that's the case I'd prefer to get rid of optional and explain in the description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not the same, in formats.py:

if sparsify is None:
    sparsify = get_option("display.multi_sparse")

index_names : bool, optional, default True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary optional

Prints the names of the indexes.
bold_rows : boolean, default False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool instead of boolean

Make the row labels bold in the output
Make the row labels bold in the output.
column_format : str, default None
The columns format as specified in `LaTeX table format
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl' for 3
columns
longtable : boolean, default will be read from the pandas config module
Default: False.
Use a longtable environment instead of tabular. Requires adding
a \\usepackage{longtable} to your LaTeX preamble.
escape : boolean, default will be read from the pandas config module
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g. 'rcl' for 3
columns.
longtable : bool, default None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be bool, optional, default None, like on line 2558?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it should be bool, optional. We never use whatever_type, optional, default None. We use only one of them.

When the None default value is being used, we use default None, imagine for example .fillna(value=None) where the None is the value used to impute.

When the None is just a flag, then we use optional. For example, in this case the longtable won't get the value None itself, but a value from the config. Meaning that it's optional to provide a longtable value, as we can use that.

When set to None, the value will default from the pandas config
module. Use a longtable environment instead of tabular. Requires
adding a \usepackage{longtable} to your LaTeX preamble.
escape : bool, default will be read from the pandas config module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you leave here simple default None, and explain about the config in the description?

Default: True.
When set to False prevents from escaping latex special
characters in column names.
encoding : str, default None
A string representing the encoding to use in the output file,
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
decimal : string, default '.'
decimal : str, default '.'
Character recognized as decimal separator, e.g. ',' in Europe.

.. versionadded:: 0.18.0

multicolumn : boolean, default True
multicolumn : bool, default True
Use \multicolumn to enhance MultiIndex columns.
The default will be read from the config module.

.. versionadded:: 0.20.0

multicolumn_format : str, default 'l'
The alignment for multicolumns, similar to `column_format`
The default will be read from the config module.

.. versionadded:: 0.20.0
multirow : bool, default False
Use \multirow to enhance MultiIndex rows. Requires adding a
\usepackage{multirow} to your LaTeX preamble. Will print
centered labels (instead of top-aligned) across the contained
rows, separating groups via clines. The default will be read
from the pandas config module.
.. versionadded:: 0.20.0

multirow : boolean, default False
Use \multirow to enhance MultiIndex rows.
Requires adding a \\usepackage{multirow} to your LaTeX preamble.
Will print centered labels (instead of top-aligned)
across the contained rows, separating groups via clines.
The default will be read from the pandas config module.
Returns
-------
str or None
If buf is None, returns the resulting LateX format as a
string. Otherwise returns None.

.. versionadded:: 0.20.0
"""
See Also
--------
DataFrame.to_csv : Write a DataFrame to CSV format.
DataFrame.to_excel : Write a DataFrame to an Excel file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to_string and to_html in this case, which to me are conceptually more similar than to_csv or to_excel (formatting to present vs formatting to export).


@Substitution(header='Write out the column names. If a list of strings '
'is given, it is assumed to be aliases for the '
'column names.')
@Appender(_shared_docs['to_latex'] % _shared_doc_kwargs)
def to_latex(self, buf=None, columns=None, col_space=None, header=True,
index=True, na_rep='NaN', formatters=None, float_format=None,
sparsify=None, index_names=True, bold_rows=False,
column_format=None, longtable=None, escape=None,
encoding=None, decimal='.', multicolumn=None,
multicolumn_format=None, multirow=None):
Examples
--------
>>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
... 'mask': ['red', 'purple'],
... 'weapon': ['sai', 'bo staff']})
>>> df.to_latex(index=False) # doctest: +NORMALIZE_WHITESPACE
'\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon
\\\\\n\\midrule\n Raphael & red & sai \\\\\n Donatello &
purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n'
"""
# Get defaults from the pandas config
if self.ndim == 1:
self = self.to_frame()
Expand Down