From ab18c6a2b8f1280f8b8f7b1dec94eb27d4b424f7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 22 Aug 2021 13:56:15 +0200 Subject: [PATCH 1/3] deprcate DataFrame.to_html() --- pandas/core/frame.py | 83 ++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c6eda015ba09f..769d794ee3563 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1010,43 +1010,44 @@ def _repr_html_(self) -> str | None: Mainly for IPython notebook. """ - if self._info_repr(): - buf = StringIO("") - self.info(buf=buf) - # need to escape the , should be the first line. - val = buf.getvalue().replace("<", r"<", 1) - val = val.replace(">", r">", 1) - return "
" + val + "
" - - if get_option("display.notebook_repr_html"): - max_rows = get_option("display.max_rows") - min_rows = get_option("display.min_rows") - max_cols = get_option("display.max_columns") - show_dimensions = get_option("display.show_dimensions") - - formatter = fmt.DataFrameFormatter( - self, - columns=None, - col_space=None, - na_rep="NaN", - formatters=None, - float_format=None, - sparsify=None, - justify=None, - index_names=True, - header=True, - index=True, - bold_rows=True, - escape=True, - max_rows=max_rows, - min_rows=min_rows, - max_cols=max_cols, - show_dimensions=show_dimensions, - decimal=".", - ) - return fmt.DataFrameRenderer(formatter).to_html(notebook=True) - else: - return None + return self.style._repr_html_() + # if self._info_repr(): + # buf = StringIO("") + # self.info(buf=buf) + # # need to escape the , should be the first line. + # val = buf.getvalue().replace("<", r"<", 1) + # val = val.replace(">", r">", 1) + # return "
" + val + "
" + # + # if get_option("display.notebook_repr_html"): + # max_rows = get_option("display.max_rows") + # min_rows = get_option("display.min_rows") + # max_cols = get_option("display.max_columns") + # show_dimensions = get_option("display.show_dimensions") + # + # formatter = fmt.DataFrameFormatter( + # self, + # columns=None, + # col_space=None, + # na_rep="NaN", + # formatters=None, + # float_format=None, + # sparsify=None, + # justify=None, + # index_names=True, + # header=True, + # index=True, + # bold_rows=True, + # escape=True, + # max_rows=max_rows, + # min_rows=min_rows, + # max_cols=max_cols, + # show_dimensions=show_dimensions, + # decimal=".", + # ) + # return fmt.DataFrameRenderer(formatter).to_html(notebook=True) + # else: + # return None @Substitution( header_type="bool or sequence", @@ -2750,6 +2751,8 @@ def to_html( ): """ Render a DataFrame as an HTML table. + + .. deprecated:: 1.4.0 %(shared_params)s bold_rows : bool, default True Make the row labels bold in the output. @@ -2776,6 +2779,12 @@ def to_html( -------- to_string : Convert DataFrame to a string. """ + warnings.warn( + "this method is deprecated in favour of `DataFrame.style.to_html()`", + FutureWarning, + stacklevel=2, + ) + if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS: raise ValueError("Invalid value for justify parameter") From 0fe70b2b50aa12cace9a28a4054e6c592af9c887 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 10:48:54 +0200 Subject: [PATCH 2/3] development of args --- pandas/core/frame.py | 214 +++++++++++++++++++++++-------------------- 1 file changed, 113 insertions(+), 101 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6a2c7249f0b98..2ec73491b8f19 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1011,43 +1011,6 @@ def _repr_html_(self) -> str | None: Mainly for IPython notebook. """ return self.style._repr_html_() - # if self._info_repr(): - # buf = StringIO("") - # self.info(buf=buf) - # # need to escape the , should be the first line. - # val = buf.getvalue().replace("<", r"<", 1) - # val = val.replace(">", r">", 1) - # return "
" + val + "
" - # - # if get_option("display.notebook_repr_html"): - # max_rows = get_option("display.max_rows") - # min_rows = get_option("display.min_rows") - # max_cols = get_option("display.max_columns") - # show_dimensions = get_option("display.show_dimensions") - # - # formatter = fmt.DataFrameFormatter( - # self, - # columns=None, - # col_space=None, - # na_rep="NaN", - # formatters=None, - # float_format=None, - # sparsify=None, - # justify=None, - # index_names=True, - # header=True, - # index=True, - # bold_rows=True, - # escape=True, - # max_rows=max_rows, - # min_rows=min_rows, - # max_cols=max_cols, - # show_dimensions=show_dimensions, - # decimal=".", - # ) - # return fmt.DataFrameRenderer(formatter).to_html(notebook=True) - # else: - # return None @Substitution( header_type="bool or sequence", @@ -2715,27 +2678,23 @@ def to_parquet( **kwargs, ) - @Substitution( - header_type="bool", - header="Whether to print column labels, default True", - col_space_type="str or int, list or dict of int or str", - col_space="The minimum width of each column in CSS length " - "units. An int is assumed to be px units.\n\n" - " .. versionadded:: 0.25.0\n" - " Ability to use str", - ) - @Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring) + @Substitution(returns=fmt.return_docstring) def to_html( self, buf: FilePathOrBuffer[str] | None = None, - columns: Sequence[str] | None = None, - col_space: ColspaceArgType | None = None, - header: bool | Sequence[str] = True, + *, + table_id: str | None = None, + table_attributes: str | None = None, + sparse_index: bool | None = None, + sparse_columns: bool | None = None, + header: bool = True, index: bool = True, + columns: Sequence[IndexLabel] | None = None, + encoding: str | None = None, + doctype_html: bool = False, na_rep: str = "NaN", formatters: FormattersType | None = None, float_format: FloatFormatType | None = None, - sparsify: bool | None = None, index_names: bool = True, justify: str | None = None, max_rows: int | None = None, @@ -2743,81 +2702,134 @@ def to_html( show_dimensions: bool | str = False, decimal: str = ".", bold_rows: bool = True, - classes: str | list | tuple | None = None, escape: bool = True, + col_space: ColspaceArgType | None = None, notebook: bool = False, border: int | None = None, - table_id: str | None = None, render_links: bool = False, - encoding: str | None = None, + classes: str | list | tuple | None = None, + sparsify: bool | None = None, ): """ Render a DataFrame as an HTML table. - .. deprecated:: 1.4.0 - %(shared_params)s + Parameters + ---------- + buf : str, Path, or StringIO-like, optional, default None + Buffer to write to. If ``None``, the output is returned as a string. + table_id : str, optional + Id attribute assigned to the HTML element in the format: + + ``
`` + table_attributes : str, optional + Attributes to assign within the `
` HTML element in the format: + + ``
>`` + + .. versionadded:: 1.4.0 + sparse_index : bool, optional + Whether to sparsify the display of a hierarchical index. Setting to False + will display each explicit level element in a hierarchical key for each row. + Defaults to ``pandas.options.styler.sparse.index`` value. + + .. versionadded:: 1.4.0 + sparse_columns : bool, optional + Whether to sparsify the display of a hierarchical index. Setting to False + will display each explicit level element in a hierarchical key for each + column. Defaults to ``pandas.options.styler.sparse.columns`` value. + + .. versionadded:: 1.4.0 + header : bool + Whether to print column headers. + + .. versionchanged:: 1.4.0 + index : bool + Whether to print index labels. + columns : sequence, optional + Subset of columns to write. Writes all columns by default. + col_space : str or int, list or dict of int or str, optional + The minimum width of each column in CSS length units. An int is assumed to + be px units. + + .. versionchanged:: 1.4.0 + encoding : str, optional + Character encoding setting for file output, and HTML meta tags, + defaults to "utf-8" if None. + + + bold_rows : bool, default True Make the row labels bold in the output. - classes : str or list or tuple, default None - CSS class(es) to apply to the resulting html table. escape : bool, default True Convert the characters <, >, and & to HTML-safe sequences. + + + classes : str or list or tuple, default None + Deprecated in favour of using ``table_attributes='class="param"'`` instead. + + .. deprecated:: 1.4.0 notebook : {True, False}, default False - Whether the generated HTML is for IPython Notebook. + Deprecated in favour of universal HTML format. + + .. deprecated:: 1.4.0 border : int - A ``border=border`` attribute is included in the opening - `
` tag. Default ``pd.options.display.html.border``. - encoding : str, default "utf-8" - Set character encoding. + Deprecated due to deprecated HTML. Use ``Styler`` and configure CSS border + properties for *table*, *th* and *td* elements instead. - .. versionadded:: 1.0 + .. deprecated:: 1.4.0 + sparsify : bool + Deprecated in favour of ``sparse_columns`` and ``sparse_index``. - table_id : str, optional - A css id is included in the opening `
` tag if specified. + .. deprecated:: 1.4.0 render_links : bool, default False - Convert URLs to HTML links. + Deprecated Convert URLs to HTML links. %(returns)s See Also -------- to_string : Convert DataFrame to a string. """ - warnings.warn( - "this method is deprecated in favour of `DataFrame.style.to_html()`", - FutureWarning, - stacklevel=2, - ) - - if justify is not None and justify not in fmt._VALID_JUSTIFY_PARAMETERS: - raise ValueError("Invalid value for justify parameter") + from pandas.io.formats.style import Styler - formatter = fmt.DataFrameFormatter( - self, - columns=columns, - col_space=col_space, - na_rep=na_rep, - header=header, - index=index, - formatters=formatters, - float_format=float_format, - bold_rows=bold_rows, - sparsify=sparsify, - justify=justify, - index_names=index_names, - escape=escape, - decimal=decimal, - max_rows=max_rows, - max_cols=max_cols, - show_dimensions=show_dimensions, - ) - # TODO: a generic formatter wld b in DataFrameFormatter - return fmt.DataFrameRenderer(formatter).to_html( + exclude_styles = True # try to remain true to legacy DataFrame.to_html + + styler = Styler(self, uuid=table_id) + styler.set_table_attributes(table_attributes) + if not header: + styler.hide_columns() + if not index: + styler.hide_index() + if columns: + hidden = [col for col in styler.columns if col not in columns] + styler.hide_columns(hidden) + + if col_space: + exclude_styles = False # CSS styles needed for col_space + props = lambda v: f"min-width: {f'{v}px' if isinstance(v, int) else v}" + + if isinstance(col_space, (int, str)): + styles = [ + {"selector": f".col{i}", "props": props(col_space)} + for i in range(len(styler.columns)) + ] + elif isinstance(col_space, list): + styles = [ + {"selector": f".col{i}", "props": props(cs)} + for i, cs in enumerate(col_space) + ] + elif isinstance(col_space, dict): + styles = { + k: [{"selector": "", "props": props(v)}] + for k, v in col_space.items() + } + styler.set_table_styles(styles, overwrite=False) + + return styler.to_html( buf=buf, - classes=classes, - notebook=notebook, - border=border, + sparse_index=sparse_index, + sparse_columns=sparse_columns, encoding=encoding, - table_id=table_id, - render_links=render_links, + doctype_html=doctype_html, + exclude_styles=exclude_styles, ) @doc(storage_options=generic._shared_docs["storage_options"]) From a11daeee3dc04ad390cf67b0af272f51fea19e7a Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 12:47:38 +0200 Subject: [PATCH 3/3] development of args --- pandas/core/frame.py | 62 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2ec73491b8f19..d47d96b0b2b33 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2690,9 +2690,14 @@ def to_html( header: bool = True, index: bool = True, columns: Sequence[IndexLabel] | None = None, + col_space: ColspaceArgType | None = None, + formatter: None = None, + na_rep: str = "NaN", + decimal: str = ".", + thousands: str | None = None, + escape: bool = True, encoding: str | None = None, doctype_html: bool = False, - na_rep: str = "NaN", formatters: FormattersType | None = None, float_format: FloatFormatType | None = None, index_names: bool = True, @@ -2700,10 +2705,7 @@ def to_html( max_rows: int | None = None, max_cols: int | None = None, show_dimensions: bool | str = False, - decimal: str = ".", bold_rows: bool = True, - escape: bool = True, - col_space: ColspaceArgType | None = None, notebook: bool = False, border: int | None = None, render_links: bool = False, @@ -2757,13 +2759,47 @@ def to_html( defaults to "utf-8" if None. - bold_rows : bool, default True Make the row labels bold in the output. - escape : bool, default True - Convert the characters <, >, and & to HTML-safe sequences. + formatter : str, callable, dict, optional + Object to define how values are displayed. See notes for ``Styler.format`` + + .. versionadded:: 1.4.0 + na_rep : str, optional + Representation for missing values. + If ``na_rep`` is None, no special formatting is applied. + + .. versionchanged:: 1.4.0 + precision : int, optional + Floating point precision to use for display purposes, if not determined by + the specified ``formatter``. + + .. versionadded:: 1.4.0 + decimal : str, default "." + Character used as decimal separator for floats, complex and integers + + .. versionadded:: 1.4.0 + thousands : str, optional, default None + Character used as thousands separator for floats, complex and integers + + .. versionadded:: 1.4.0 + escape : bool, + Replaces the characters ``&``, ``<``, ``>``, ``'``, and ``"`` + in cell display string with HTML-safe sequences. + Escaping is done before ``formatter``. + + .. versionchanged:: 1.4.0 + + formatters : list, tuple or dict of one-param. functions, optional + Deprecated in favour of using ``formatter`` which is an argument to + ``Styler.format``. + .. deprecated:: 1.4.0 + float_format : one-parameter function, optional, default None + Deprecated in favour of using arguments native to ``Styler.format`` + + .. deprecated:: 1.4.0 classes : str or list or tuple, default None Deprecated in favour of using ``table_attributes='class="param"'`` instead. @@ -2792,7 +2828,17 @@ def to_html( exclude_styles = True # try to remain true to legacy DataFrame.to_html - styler = Styler(self, uuid=table_id) + styler = Styler( + self, + uuid=table_id, + formatter=formatter, + na_rep=na_rep, + precision=precision, + decimal=decimal, + thousands=thousands, + escape="html" if escape else None, + ) + styler.set_table_attributes(table_attributes) if not header: styler.hide_columns()