|
45 | 45 | import pandas.core.common as com
|
46 | 46 | import pandas.core.missing as missing
|
47 | 47 | from pandas.io.formats.printing import pprint_thing
|
48 |
| -from pandas.io.formats.format import format_percentiles |
| 48 | +from pandas.io.formats.format import format_percentiles, DataFrameFormatter |
49 | 49 | from pandas.tseries.frequencies import to_offset
|
50 | 50 | from pandas import compat
|
51 | 51 | from pandas.compat.numpy import function as nv
|
@@ -1502,6 +1502,95 @@ def to_xarray(self):
|
1502 | 1502 | coords=coords,
|
1503 | 1503 | )
|
1504 | 1504 |
|
| 1505 | + _shared_docs['to_latex'] = """ |
| 1506 | + Render an object to a tabular environment table. You can splice |
| 1507 | + this into a LaTeX document. Requires \\usepackage{booktabs}. |
| 1508 | +
|
| 1509 | + `to_latex`-specific options: |
| 1510 | +
|
| 1511 | + bold_rows : boolean, default True |
| 1512 | + Make the row labels bold in the output |
| 1513 | + column_format : str, default None |
| 1514 | + The columns format as specified in `LaTeX table format |
| 1515 | + <https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl' for 3 |
| 1516 | + columns |
| 1517 | + longtable : boolean, default will be read from the pandas config module |
| 1518 | + Default: False. |
| 1519 | + Use a longtable environment instead of tabular. Requires adding |
| 1520 | + a \\usepackage{longtable} to your LaTeX preamble. |
| 1521 | + escape : boolean, default will be read from the pandas config module |
| 1522 | + Default: True. |
| 1523 | + When set to False prevents from escaping latex special |
| 1524 | + characters in column names. |
| 1525 | + encoding : str, default None |
| 1526 | + A string representing the encoding to use in the output file, |
| 1527 | + defaults to 'ascii' on Python 2 and 'utf-8' on Python 3. |
| 1528 | + decimal : string, default '.' |
| 1529 | + Character recognized as decimal separator, e.g. ',' in Europe. |
| 1530 | +
|
| 1531 | + .. versionadded:: 0.18.0 |
| 1532 | +
|
| 1533 | + multicolumn : boolean, default True |
| 1534 | + Use \multicolumn to enhance MultiIndex columns. |
| 1535 | + The default will be read from the config module. |
| 1536 | +
|
| 1537 | + .. versionadded:: 0.20.0 |
| 1538 | +
|
| 1539 | + multicolumn_format : str, default 'l' |
| 1540 | + The alignment for multicolumns, similar to `column_format` |
| 1541 | + The default will be read from the config module. |
| 1542 | +
|
| 1543 | + .. versionadded:: 0.20.0 |
| 1544 | +
|
| 1545 | + multirow : boolean, default False |
| 1546 | + Use \multirow to enhance MultiIndex rows. |
| 1547 | + Requires adding a \\usepackage{multirow} to your LaTeX preamble. |
| 1548 | + Will print centered labels (instead of top-aligned) |
| 1549 | + across the contained rows, separating groups via clines. |
| 1550 | + The default will be read from the pandas config module. |
| 1551 | +
|
| 1552 | + .. versionadded:: 0.20.0 |
| 1553 | + """ |
| 1554 | + |
| 1555 | + @Substitution(header='Write out column names. If a list of string is given, \ |
| 1556 | +it is assumed to be aliases for the column names.') |
| 1557 | + @Appender(_shared_docs['to_latex'] % _shared_doc_kwargs) |
| 1558 | + def to_latex(self, buf=None, columns=None, col_space=None, header=True, |
| 1559 | + index=True, na_rep='NaN', formatters=None, float_format=None, |
| 1560 | + sparsify=None, index_names=True, bold_rows=True, |
| 1561 | + column_format=None, longtable=None, escape=None, |
| 1562 | + encoding=None, decimal='.', multicolumn=None, |
| 1563 | + multicolumn_format=None, multirow=None): |
| 1564 | + # Get defaults from the pandas config |
| 1565 | + if longtable is None: |
| 1566 | + longtable = config.get_option("display.latex.longtable") |
| 1567 | + if escape is None: |
| 1568 | + escape = config.get_option("display.latex.escape") |
| 1569 | + if multicolumn is None: |
| 1570 | + multicolumn = config.get_option("display.latex.multicolumn") |
| 1571 | + if multicolumn_format is None: |
| 1572 | + multicolumn_format = config.get_option( |
| 1573 | + "display.latex.multicolumn_format") |
| 1574 | + if multirow is None: |
| 1575 | + multirow = config.get_option("display.latex.multirow") |
| 1576 | + |
| 1577 | + formatter = DataFrameFormatter(self, buf=buf, columns=columns, |
| 1578 | + col_space=col_space, na_rep=na_rep, |
| 1579 | + header=header, index=index, |
| 1580 | + formatters=formatters, |
| 1581 | + float_format=float_format, |
| 1582 | + bold_rows=bold_rows, |
| 1583 | + sparsify=sparsify, |
| 1584 | + index_names=index_names, |
| 1585 | + escape=escape, decimal=decimal) |
| 1586 | + formatter.to_latex(column_format=column_format, longtable=longtable, |
| 1587 | + encoding=encoding, multicolumn=multicolumn, |
| 1588 | + multicolumn_format=multicolumn_format, |
| 1589 | + multirow=multirow) |
| 1590 | + |
| 1591 | + if buf is None: |
| 1592 | + return formatter.buf.getvalue() |
| 1593 | + |
1505 | 1594 | # ----------------------------------------------------------------------
|
1506 | 1595 | # Fancy Indexing
|
1507 | 1596 |
|
|
0 commit comments