Skip to content

WARN: Add FutureWarning to DataFrame.to_html #44451

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

Closed
Closed
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2fa2246
add warning
attack68 Nov 12, 2021
89225de
add test
attack68 Nov 12, 2021
1119a27
fix doc warnings
attack68 Nov 12, 2021
93d9288
fix test for warning
attack68 Nov 12, 2021
2e653fe
filterwarnings
attack68 Nov 13, 2021
8e10574
skip doctest
attack68 Nov 13, 2021
e4fa4ef
test warnings
attack68 Nov 13, 2021
e20c5d9
test warnings
attack68 Nov 13, 2021
e6fc67e
Merge remote-tracking branch 'upstream/master' into depr_to_html_to_l…
attack68 Nov 14, 2021
310bdc2
warn and expand tests
attack68 Nov 14, 2021
3524116
see also
attack68 Nov 14, 2021
43acc91
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Nov 24, 2021
f41a438
allow warnings in io.rst
attack68 Nov 24, 2021
ad8cf24
allow warnings in scale.rst
attack68 Nov 24, 2021
0d5b4b3
aedit io.rst
attack68 Nov 24, 2021
bd5e91c
aedit io.rst
attack68 Nov 24, 2021
7e3c3df
test warnings
attack68 Nov 25, 2021
7de29d1
remove okwarning
attack68 Nov 25, 2021
f2a944a
pytest filters
attack68 Nov 25, 2021
5f4d55c
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Nov 26, 2021
6a4523e
whats new
attack68 Nov 26, 2021
9e66a72
add explicit test
attack68 Nov 26, 2021
505953d
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Dec 7, 2021
d859877
more specific warnings
attack68 Dec 7, 2021
02d913c
more specific warnings
attack68 Dec 7, 2021
7d5ab6c
more specific warnings (filters removed)
attack68 Dec 7, 2021
a9e4ac4
more specific warnings (filters removed)
attack68 Dec 7, 2021
ee97624
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Dec 15, 2021
10a34cb
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Dec 18, 2021
272670a
doc string addition
attack68 Dec 22, 2021
3af20b6
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Dec 22, 2021
6962393
Merge remote-tracking branch 'upstream/master' into depr_to_html_afte…
attack68 Jan 2, 2022
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
Prev Previous commit
Next Next commit
aedit io.rst
  • Loading branch information
attack68 committed Nov 24, 2021
commit 0d5b4b381cddab705c227b84b5111f82b7f0582a
159 changes: 15 additions & 144 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The pandas I/O API is a set of top level ``reader`` functions accessed like
text;`CSV <https://en.wikipedia.org/wiki/Comma-separated_values>`__;:ref:`read_csv<io.read_csv_table>`;:ref:`to_csv<io.store_in_csv>`
text;Fixed-Width Text File;:ref:`read_fwf<io.fwf_reader>`
text;`JSON <https://www.json.org/>`__;:ref:`read_json<io.json_reader>`;:ref:`to_json<io.json_writer>`
text;`HTML <https://en.wikipedia.org/wiki/HTML>`__;:ref:`read_html<io.read_html>`;:ref:`to_html<io.html>`
text;`HTML <https://en.wikipedia.org/wiki/HTML>`__;:ref:`read_html<io.read_html>`;:ref:`Styler.to_html<io.html>`
text;`LaTeX <https://en.wikipedia.org/wiki/LaTeX>`__;;:ref:`Styler.to_latex<io.latex>`
text;`XML <https://www.w3.org/standards/xml/core>`__;:ref:`read_xml<io.read_xml>`;:ref:`to_xml<io.xml>`
text; Local clipboard;:ref:`read_clipboard<io.clipboard>`;:ref:`to_clipboard<io.clipboard>`
Expand Down Expand Up @@ -2666,163 +2666,34 @@ succeeds, the function will return*.
Writing to HTML files
''''''''''''''''''''''

``DataFrame`` objects have an instance method ``to_html`` which renders the
contents of the ``DataFrame`` as an HTML table. The function arguments are as
in the method ``to_string`` described above.

.. note::

Not all of the possible options for ``DataFrame.to_html`` are shown here for
brevity's sake. See :func:`~pandas.core.frame.DataFrame.to_html` for the
full set of options.
DataFrame *and* Styler objects currently have a ``to_html`` method. We recommend
using the `Styler.to_html() <../reference/api/pandas.io.formats.style.Styler.to_html.rst>`__ method
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 use the sphinx cross-reference syntax instead of the path-based link? (:meth:`Styler.to_html() <pandas.io.formats.style.Styler.to_html>` for the long format and controlling which text is changes, but I think in this case you can also use :meth:`.Styler.to_html` to get exactly the same)

(and the same for similar cases below)

over `DataFrame.to_html() <../reference/api/pandas.DataFrame.to_html.rst>`__ due to the former's greater flexibility with
conditional styling, and the latter's possible future deprecation.

.. ipython:: python
:suppress:
:okwarning:
Review the documentation for `Styler.to_html <../reference/api/pandas.io.formats.style.Styler.to_html.rst>`__,
which gives examples of conditional styling and explains the operation of its keyword
arguments. The ``to_html`` methods render the contents of the ``DataFrame`` as an HTML table.

def write_html(df, filename, *args, **kwargs):
static = os.path.abspath(os.path.join("source", "_static"))
with open(os.path.join(static, filename + ".html"), "w") as f:
df.to_html(f, *args, **kwargs)
For simple application the following pattern is sufficient:

.. ipython:: python
:okwarning:

df = pd.DataFrame(np.random.randn(2, 2))
df
print(df.to_html()) # raw html

.. ipython:: python
:suppress:

write_html(df, "basic")

HTML:

.. raw:: html
:file: ../_static/basic.html

The ``columns`` argument will limit the columns shown:

.. ipython:: python
:okwarning:

print(df.to_html(columns=[0]))

.. ipython:: python
:suppress:
print(df.style.to_html()) # raw html

write_html(df, "columns", columns=[0])

HTML:

.. raw:: html
:file: ../_static/columns.html

``float_format`` takes a Python callable to control the precision of floating
point values:

.. ipython:: python
:okwarning:

print(df.to_html(float_format="{0:.10f}".format))

.. ipython:: python
:suppress:

write_html(df, "float_format", float_format="{0:.10f}".format)

HTML:

.. raw:: html
:file: ../_static/float_format.html

``bold_rows`` will make the row labels bold by default, but you can turn that
off:

.. ipython:: python
:okwarning:

print(df.to_html(bold_rows=False))

.. ipython:: python
:suppress:

write_html(df, "nobold", bold_rows=False)

.. raw:: html
:file: ../_static/nobold.html

The ``classes`` argument provides the ability to give the resulting HTML
table CSS classes. Note that these classes are *appended* to the existing
``'dataframe'`` class.

.. ipython:: python
:okwarning:

print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"]))

The ``render_links`` argument provides the ability to add hyperlinks to cells
that contain URLs.

.. ipython:: python
:okwarning:

url_df = pd.DataFrame(
{
"name": ["Python", "pandas"],
"url": ["https://www.python.org/", "https://pandas.pydata.org"],
}
)
print(url_df.to_html(render_links=True))

.. ipython:: python
:suppress:

write_html(url_df, "render_links", render_links=True)

HTML:

.. raw:: html
:file: ../_static/render_links.html

Finally, the ``escape`` argument allows you to control whether the
"<", ">" and "&" characters escaped in the resulting HTML (by default it is
``True``). So to get the HTML without escaped characters pass ``escape=False``

.. ipython:: python

df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)})


.. ipython:: python
:suppress:

write_html(df, "escape")
write_html(df, "noescape", escape=False)

Escaped:

.. ipython:: python

print(df.to_html())

.. raw:: html
:file: ../_static/escape.html

Not escaped:
To format values before output, chain the `Styler.format <../reference/api/pandas.io.formats.style.Styler.format.rst>`__
method.

.. ipython:: python

print(df.to_html(escape=False))

.. raw:: html
:file: ../_static/noescape.html

.. note::
print(df.style.format("€ {}").to_html())

Some browsers may not show a difference in the rendering of the previous two
HTML tables.
Some browsers or browser applications may process and add css class styling by default to alter the appearance
of HTML tables, such as Jupyter Notebook and Google Colab.


.. _io.html.gotchas:
Expand Down