diff --git a/ci/deps/actions-38-db-min.yaml b/ci/deps/actions-38-db-min.yaml index c93f791b7dba7..55489a41f7997 100644 --- a/ci/deps/actions-38-db-min.yaml +++ b/ci/deps/actions-38-db-min.yaml @@ -15,6 +15,7 @@ dependencies: - numpy<1.20 # GH#39541 compat for pyarrow<3 - python-dateutil - pytz + - jinja2 # optional - beautifulsoup4 diff --git a/ci/deps/actions-38-db.yaml b/ci/deps/actions-38-db.yaml index b4495fa6887f4..d6c37cf62dad8 100644 --- a/ci/deps/actions-38-db.yaml +++ b/ci/deps/actions-38-db.yaml @@ -20,6 +20,7 @@ dependencies: - gcsfs>=0.6.0 - geopandas - html5lib + - jinja2 - matplotlib - moto>=1.3.14 - flask diff --git a/ci/deps/actions-38-locale_slow.yaml b/ci/deps/actions-38-locale_slow.yaml index e7276027f2a41..b55cc935e131d 100644 --- a/ci/deps/actions-38-locale_slow.yaml +++ b/ci/deps/actions-38-locale_slow.yaml @@ -28,3 +28,4 @@ dependencies: - xlsxwriter=1.2.2 - xlwt=1.3.0 - html5lib=1.1 + - jinja2 diff --git a/ci/deps/actions-38-slow.yaml b/ci/deps/actions-38-slow.yaml index 08900a31fe27c..39fe7ea7b086a 100644 --- a/ci/deps/actions-38-slow.yaml +++ b/ci/deps/actions-38-slow.yaml @@ -36,3 +36,4 @@ dependencies: - moto - flask - numba + - jinja2 diff --git a/ci/deps/actions-38.yaml b/ci/deps/actions-38.yaml index 86b038ff7d4b6..273d9e2d016ae 100644 --- a/ci/deps/actions-38.yaml +++ b/ci/deps/actions-38.yaml @@ -18,3 +18,4 @@ dependencies: - nomkl - pytz - tabulate==0.8.7 + - jinja2 diff --git a/ci/deps/actions-39-numpydev.yaml b/ci/deps/actions-39-numpydev.yaml index 03181a9d71d1d..18fe197eb0a24 100644 --- a/ci/deps/actions-39-numpydev.yaml +++ b/ci/deps/actions-39-numpydev.yaml @@ -13,6 +13,7 @@ dependencies: # pandas dependencies - python-dateutil - pytz + - jinja2 - pip - pip: - cython==0.29.21 # GH#34014 diff --git a/doc/source/getting_started/install.rst b/doc/source/getting_started/install.rst index 20ae37c85a9d9..99b96b62e314e 100644 --- a/doc/source/getting_started/install.rst +++ b/doc/source/getting_started/install.rst @@ -227,6 +227,7 @@ Package Minimum support `NumPy `__ 1.18.5 `python-dateutil `__ 2.8.1 `pytz `__ 2020.1 +`Jinja2 `__ 2.11 ================================================================ ========================== .. _install.recommended_dependencies: @@ -266,7 +267,6 @@ Visualization Dependency Minimum Version Notes ========================= ================== ============================================================= matplotlib 3.3.2 Plotting library -Jinja2 2.11 Conditional formatting with DataFrame.style tabulate 0.8.7 Printing in Markdown-friendly format (see `tabulate`_) ========================= ================== ============================================================= diff --git a/environment.yml b/environment.yml index 733bd06fbe12f..2199dd690d828 100644 --- a/environment.yml +++ b/environment.yml @@ -7,6 +7,7 @@ dependencies: - python=3.8 - python-dateutil>=2.8.1 - pytz + - jinja2 # pandas.Styler (DataFrame.to_html / to_latex) # benchmarks - asv @@ -79,7 +80,6 @@ dependencies: - bottleneck>=1.3.1 - ipykernel - ipython>=7.11.1 - - jinja2 # pandas.Styler - matplotlib>=3.3.2 # pandas.plotting, Series.plot, DataFrame.plot - numexpr>=2.7.1 - scipy>=1.4.1 diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e171ded654989..f3387aa0ad9e7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -26,7 +26,10 @@ import numpy as np -from pandas._config import config +from pandas._config import ( + config, + get_option, +) from pandas._libs import lib from pandas._libs.tslibs import ( @@ -2108,7 +2111,7 @@ def _repr_latex_(self): Returns a LaTeX representation for a particular object. Mainly for use with nbconvert (jupyter notebook conversion to pdf). """ - if config.get_option("display.latex.repr"): + if config.get_option("styler.render.repr") == "latex": return self.to_latex() else: return None @@ -3116,32 +3119,45 @@ class (index) object 'bird' 'bird' 'mammal' 'mammal' def to_latex( self, buf=None, + *, + hrules=False, 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, + position=None, + position_float=None, + caption=None, + label=None, + index=True, + header=True, + index_names="all", + sparse_index=None, + sparse_columns=None, + multirow_align=None, + multicol_align=None, + siunitx=False, + environment=None, + formatter=None, + precision=None, + na_rep=None, + decimal=None, + thousands=None, escape=None, + bold_header="none", encoding=None, - decimal=".", + formatters=None, + float_format=None, multicolumn=None, multicolumn_format=None, multirow=None, - caption=None, - label=None, - position=None, + longtable=None, + sparsify=None, + col_space=None, + bold_rows=None, ): r""" Render object to a LaTeX tabular, longtable, or nested table/tabular. - Requires ``\usepackage{{booktabs}}``. The output can be copy/pasted + The output can be copy/pasted into a main LaTeX document or read from an external file with ``\input{{table.tex}}``. @@ -3151,150 +3167,338 @@ def to_latex( .. versionchanged:: 1.2.0 Added position argument, changed meaning of caption argument. + .. versionchanged:: 1.4.0 + Now uses ``Styler.to_latex`` implementation via jinja2 templating. + Significant changes to arguments. See Notes. + Parameters ---------- - buf : str, Path or StringIO-like, optional, default None - Buffer to write to. If None, the output is returned as a string. + buf : str, Path or StringIO-like, optional + Buffer to write to. If `None`, the output is returned as a string. + hrules : bool + Set to `True` to add `\\toprule`, `\\midrule` and `\\bottomrule` from the + `booktabs` LaTeX package. + + .. versionadded:: 1.4.0 columns : list of label, optional 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 of functions or dict of {{str: function}}, optional - 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 : one-parameter function or str, optional, default None - Formatter for floating point numbers. For example - ``float_format="%.2f"`` and ``float_format="{{:0.2f}}".format`` will - both result in 0.1234 being formatted as 0.12. - sparsify : bool, optional - Set to False for a DataFrame with a hierarchical index to print - every multiindex key at each row. By default, the value will be - read from the config module. - index_names : bool, default True - Prints the names of the indexes. - bold_rows : bool, default False - Make the row labels bold in the output. column_format : str, optional - The columns format as specified in `LaTeX table format - `__ e.g. 'rcl' for 3 - columns. By default, 'l' will be used for all columns except - columns of numbers, which default to 'r'. - longtable : bool, optional - By default, the value will be read from the pandas config - module. Use a longtable environment instead of tabular. Requires - adding a \usepackage{{longtable}} to your LaTeX preamble. - escape : bool, optional - By default, the value will be read from the pandas config - module. When set to False prevents from escaping latex special - characters in column names. + The LaTeX column specification placed in location: + + `\\begin{{tabular}}{{}}` + + Defaults to 'l' for index and + non-numeric data columns, and, for numeric data columns, + to 'r' by default, or 'S' if ``siunitx`` is `True`. + + .. versionchanged:: 1.4.0 + position : str, optional + The LaTeX positional argument (e.g. 'h!') for tables, placed in location: + + `\\begin{{table}}[]`. + + .. versionchanged:: 1.2.0 + position_float : {{"centering", "raggedleft", "raggedright"}}, optional + The LaTeX float command placed in location: + + `\\begin{{table}}[]` + + `\\` + + Cannot be used if ``environment`` is "longtable". + + .. versionadded:: 1.4.0 + caption : str, tuple, optional + If string, then table caption included as: `\\caption{{}}`. + If tuple, i.e ("full caption", "short caption"), the caption included + as: + + `\\caption[]{{}}`. + + .. versionadded:: 1.0.0 + + .. versionchanged:: 1.2.0 + Optionally allow caption to be a tuple `(full_caption, short_caption)`. + label : str, optional + The LaTeX label included as: `\\label{{