|
46 | 46 | import pandas.core.common as com
|
47 | 47 | import pandas.core.missing as missing
|
48 | 48 | from pandas.io.formats.printing import pprint_thing
|
49 |
| -from pandas.io.formats.format import format_percentiles |
| 49 | +from pandas.io.formats.format import format_percentiles, DataFrameFormatter |
50 | 50 | from pandas.tseries.frequencies import to_offset
|
51 | 51 | from pandas import compat
|
52 | 52 | from pandas.compat.numpy import function as nv
|
@@ -1051,6 +1051,16 @@ def __setstate__(self, state):
|
1051 | 1051 | # ----------------------------------------------------------------------
|
1052 | 1052 | # IO
|
1053 | 1053 |
|
| 1054 | + def _repr_latex_(self): |
| 1055 | + """ |
| 1056 | + Returns a LaTeX representation for a particular object. |
| 1057 | + Mainly for use with nbconvert (jupyter notebook conversion to pdf). |
| 1058 | + """ |
| 1059 | + if config.get_option('display.latex.repr'): |
| 1060 | + return self.to_latex() |
| 1061 | + else: |
| 1062 | + return None |
| 1063 | + |
1054 | 1064 | # ----------------------------------------------------------------------
|
1055 | 1065 | # I/O Methods
|
1056 | 1066 |
|
@@ -1489,6 +1499,100 @@ def to_xarray(self):
|
1489 | 1499 | coords=coords,
|
1490 | 1500 | )
|
1491 | 1501 |
|
| 1502 | + _shared_docs['to_latex'] = """ |
| 1503 | + Render an object to a tabular environment table. You can splice |
| 1504 | + this into a LaTeX document. Requires \\usepackage{booktabs}. |
| 1505 | +
|
| 1506 | + .. versionchanged:: 0.20.2 |
| 1507 | + Added to Series |
| 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 self.ndim == 1: |
| 1566 | + self = self.to_frame() |
| 1567 | + if longtable is None: |
| 1568 | + longtable = config.get_option("display.latex.longtable") |
| 1569 | + if escape is None: |
| 1570 | + escape = config.get_option("display.latex.escape") |
| 1571 | + if multicolumn is None: |
| 1572 | + multicolumn = config.get_option("display.latex.multicolumn") |
| 1573 | + if multicolumn_format is None: |
| 1574 | + multicolumn_format = config.get_option( |
| 1575 | + "display.latex.multicolumn_format") |
| 1576 | + if multirow is None: |
| 1577 | + multirow = config.get_option("display.latex.multirow") |
| 1578 | + |
| 1579 | + formatter = DataFrameFormatter(self, buf=buf, columns=columns, |
| 1580 | + col_space=col_space, na_rep=na_rep, |
| 1581 | + header=header, index=index, |
| 1582 | + formatters=formatters, |
| 1583 | + float_format=float_format, |
| 1584 | + bold_rows=bold_rows, |
| 1585 | + sparsify=sparsify, |
| 1586 | + index_names=index_names, |
| 1587 | + escape=escape, decimal=decimal) |
| 1588 | + formatter.to_latex(column_format=column_format, longtable=longtable, |
| 1589 | + encoding=encoding, multicolumn=multicolumn, |
| 1590 | + multicolumn_format=multicolumn_format, |
| 1591 | + multirow=multirow) |
| 1592 | + |
| 1593 | + if buf is None: |
| 1594 | + return formatter.buf.getvalue() |
| 1595 | + |
1492 | 1596 | # ----------------------------------------------------------------------
|
1493 | 1597 | # Fancy Indexing
|
1494 | 1598 |
|
|
0 commit comments