Skip to content

REF: collect to_string tests #55751

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 7 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions doc/source/development/contributing_codebase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ be located.

- tests.io

.. note::

This includes ``to_string`` but excludes ``__repr__``, which is
tested in ``tests.frame.test_repr`` and ``tests.series.test_repr``.
Other classes often have a ``test_formats`` file.

C) Otherwise
This test likely belongs in one of:

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/frame/methods/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,27 @@ def test_info_compute_numba():
df.info()
expected = buf.getvalue()
assert result == expected


@pytest.mark.parametrize(
"row, columns, show_counts, result",
[
[20, 20, None, True],
[20, 20, True, True],
[20, 20, False, False],
[5, 5, None, False],
[5, 5, True, False],
[5, 5, False, False],
],
)
def test_info_show_counts(row, columns, show_counts, result):
# Explicit cast to float to avoid implicit cast when setting nan
df = DataFrame(1, columns=range(10), index=range(10)).astype({1: "float"})
df.iloc[1, 1] = np.nan

with option_context(
"display.max_info_rows", row, "display.max_info_columns", columns
):
with StringIO() as buf:
df.info(buf=buf, show_counts=show_counts)
assert ("non-null" in buf.getvalue()) is result
50 changes: 36 additions & 14 deletions pandas/tests/frame/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,20 +422,6 @@ def test_to_records_with_inf_record(self):
result = repr(df)
assert result == expected

def test_masked_ea_with_formatter(self):
# GH#39336
df = DataFrame(
{
"a": Series([0.123456789, 1.123456789], dtype="Float64"),
"b": Series([1, 2], dtype="Int64"),
}
)
result = df.to_string(formatters=["{:.2f}".format, "{:.2f}".format])
expected = """ a b
0 0.12 1.00
1 1.12 2.00"""
assert result == expected

def test_repr_ea_columns(self, any_string_dtype):
# GH#54797
pytest.importorskip("pyarrow")
Expand All @@ -446,3 +432,39 @@ def test_repr_ea_columns(self, any_string_dtype):
1 2 5
2 3 6"""
assert repr(df) == expected


@pytest.mark.parametrize(
"data,output",
[
([2, complex("nan"), 1], [" 2.0+0.0j", " NaN+0.0j", " 1.0+0.0j"]),
([2, complex("nan"), -1], [" 2.0+0.0j", " NaN+0.0j", "-1.0+0.0j"]),
([-2, complex("nan"), -1], ["-2.0+0.0j", " NaN+0.0j", "-1.0+0.0j"]),
([-1.23j, complex("nan"), -1], ["-0.00-1.23j", " NaN+0.00j", "-1.00+0.00j"]),
([1.23j, complex("nan"), 1.23], [" 0.00+1.23j", " NaN+0.00j", " 1.23+0.00j"]),
(
[-1.23j, complex(np.nan, np.nan), 1],
["-0.00-1.23j", " NaN+ NaNj", " 1.00+0.00j"],
),
(
[-1.23j, complex(1.2, np.nan), 1],
["-0.00-1.23j", " 1.20+ NaNj", " 1.00+0.00j"],
),
(
[-1.23j, complex(np.nan, -1.2), 1],
["-0.00-1.23j", " NaN-1.20j", " 1.00+0.00j"],
),
],
)
@pytest.mark.parametrize("as_frame", [True, False])
def test_repr_with_complex_nans(data, output, as_frame):
# GH#53762, GH#53841
obj = Series(np.array(data))
if as_frame:
obj = obj.to_frame(name="val")
reprs = [f"{i} {val}" for i, val in enumerate(output)]
expected = f"{'val': >{len(reprs[0])}}\n" + "\n".join(reprs)
else:
reprs = [f"{i} {val}" for i, val in enumerate(output)]
expected = "\n".join(reprs) + "\ndtype: complex128"
assert str(obj) == expected, f"\n{str(obj)}\n\n{expected}"
Loading