Skip to content

TST: move and reformat latex_na_rep test #37717

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 1 commit into from
Nov 9, 2020
Merged
Changes from all 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
45 changes: 24 additions & 21 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,30 @@ def test_to_latex_float_format_no_fixed_width_integer(self):
)
assert result == expected

@pytest.mark.parametrize("na_rep", ["NaN", "Ted"])
def test_to_latex_na_rep_and_float_format(self, na_rep):
df = DataFrame(
[
["A", 1.2225],
["A", None],
],
columns=["Group", "Data"],
)
result = df.to_latex(na_rep=na_rep, float_format="{:.2f}".format)
expected = _dedent(
Copy link
Member

Choose a reason for hiding this comment

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

Nice. I think this same pattern appears in other places, could push this definition higher up and use elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean _dedent (without the preceding newline?)

Copy link
Member

@dsaxton dsaxton Nov 9, 2020

Choose a reason for hiding this comment

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

Ah, maybe the other cases aren't using the preceding newline so could probably use dedent alone for those

Copy link
Member Author

@ivanovmg ivanovmg Nov 9, 2020

Choose a reason for hiding this comment

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

Here I used custom _dedent only to make it compatible with raw strings.
In general I would simply use this

dedent(
    """\
    content
    """
)

I guess that raw strings are mostly present here, in latex-related tests, because of the number of backslashes.
In other places we may want to use just built-in dedent

fr"""
\begin{{tabular}}{{llr}}
\toprule
{{}} & Group & Data \\
\midrule
0 & A & 1.22 \\
1 & A & {na_rep} \\
\bottomrule
\end{{tabular}}
"""
)
assert result == expected


class TestToLatexMultiindex:
@pytest.fixture
Expand Down Expand Up @@ -1431,24 +1455,3 @@ def test_get_strrow_multindex_multicolumn(self, row_num, expected):
)

assert row_string_converter.get_strrow(row_num=row_num) == expected

@pytest.mark.parametrize("na_rep", ["NaN", "Ted"])
def test_to_latex_na_rep_and_float_format(self, na_rep):
df = DataFrame(
[
["A", 1.2225],
["A", None],
],
columns=["Group", "Data"],
)
result = df.to_latex(na_rep=na_rep, float_format="{:.2f}".format)
expected = f"""\\begin{{tabular}}{{llr}}
\\toprule
{{}} & Group & Data \\\\
\\midrule
0 & A & 1.22 \\\\
1 & A & {na_rep} \\\\
\\bottomrule
\\end{{tabular}}
"""
assert result == expected