Skip to content

TST: Fix for doctest in style_render.py -> pandas.io.formats.style_re… #42778

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

Merged
merged 4 commits into from
Aug 1, 2021
Merged
Changes from 2 commits
Commits
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
16 changes: 9 additions & 7 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,21 +646,22 @@ def format(
Using ``na_rep`` and ``precision`` with the default ``formatter``

>>> df = pd.DataFrame([[np.nan, 1.0, 'A'], [2.0, np.nan, 3.0]])
>>> df.style.format(na_rep='MISS', precision=3)
>>> df.style.format(na_rep='MISS', precision=3) # doctest: +SKIP
0 1 2
0 MISS 1.000 A
1 2.000 MISS 3.000

Using a ``formatter`` specification on consistent column dtypes

>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1])
>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1]) # doctest: +SKIP
0 1 2
0 MISS 1.00 A
1 2.00 MISS 3.000000

Using the default ``formatter`` for unspecified columns

>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
... # doctest: +SKIP
0 1 2
0 MISS £ 1.0 A
1 2.00 MISS 3.0
Expand All @@ -669,15 +670,16 @@ def format(
``formatter``.

>>> df.style.format(na_rep='MISS', precision=1, subset=[0])
... .format(na_rep='PASS', precision=2, subset=[1, 2])
... .format(na_rep='PASS', precision=2, subset=[1, 2]) # doctest: +SKIP
0 1 2
0 MISS 1.00 A
1 2.0 PASS 3.00

Using a callable ``formatter`` function.

>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
>>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
>>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
... # doctest: +SKIP
0 1 2
0 MISS 1.0000 STRING
1 2.0 MISS FLOAT
Expand All @@ -688,7 +690,7 @@ def format(
>>> s = df.style.format(
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
... )
>>> s.render()
>>> s.render() # doctest: +SKIP
...
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
<td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
Expand All @@ -698,7 +700,7 @@ def format(
Using a ``formatter`` with LaTeX ``escape``.

>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
>>> s = df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
>>> s = df.style.format("\\textbf{{{}}}", escape="latex").to_latex() # doctest: +SKIP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_latex() actually produces a string result.. maybe this could be doctested.

Copy link
Contributor Author

@aneesh98 aneesh98 Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command line output I got by running this test is not pretty printed actually as it is in the docs, thus failing the doctest:

________ [doctest] pandas.io.formats.style_render.StylerRenderer.format ________
694         ...
695         <td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
696         <td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
697         <td .. >NA</td>
698         ...
699 
700         Using a ``formatter`` with LaTeX ``escape``.
701 
702         >>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
703         >>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
Expected:
    \begin{tabular}{ll}
    {} & {0} \\
    0 & \textbf{123} \\
    1 & \textbf{\textasciitilde \space \textasciicircum } \\
    2 & \textbf{\$\%\#} \\
    \end{tabular}
Got:
    '\\begin{tabular}{ll}\n{} & {0} \\\\\n0 & \\textbf{123} \\\\\n1 & \\textbf{\\textasciitilde \\space \\textasciicircum } \\\\\n2 & \\textbf{\\$\\%\\#} \\\\\n\\end{tabular}\n'

Please let me know if I am missing something here.
Thanks.

\begin{tabular}{ll}
{} & {0} \\
0 & \textbf{123} \\
Expand Down