Skip to content

Commit 5180fee

Browse files
authored
REF: collect to_string tests (#55751)
* REF: misplaced repr tests * misplaced test * Collect to_string tests with formatters * Collect to_string tests by keyword * collect to_string tests * collect to_string tests * collect to_string tests
1 parent adae693 commit 5180fee

File tree

7 files changed

+1314
-1282
lines changed

7 files changed

+1314
-1282
lines changed

doc/source/development/contributing_codebase.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ be located.
456456

457457
- tests.io
458458

459+
.. note::
460+
461+
This includes ``to_string`` but excludes ``__repr__``, which is
462+
tested in ``tests.frame.test_repr`` and ``tests.series.test_repr``.
463+
Other classes often have a ``test_formats`` file.
464+
459465
C) Otherwise
460466
This test likely belongs in one of:
461467

pandas/tests/frame/methods/test_info.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,27 @@ def test_info_compute_numba():
539539
df.info()
540540
expected = buf.getvalue()
541541
assert result == expected
542+
543+
544+
@pytest.mark.parametrize(
545+
"row, columns, show_counts, result",
546+
[
547+
[20, 20, None, True],
548+
[20, 20, True, True],
549+
[20, 20, False, False],
550+
[5, 5, None, False],
551+
[5, 5, True, False],
552+
[5, 5, False, False],
553+
],
554+
)
555+
def test_info_show_counts(row, columns, show_counts, result):
556+
# Explicit cast to float to avoid implicit cast when setting nan
557+
df = DataFrame(1, columns=range(10), index=range(10)).astype({1: "float"})
558+
df.iloc[1, 1] = np.nan
559+
560+
with option_context(
561+
"display.max_info_rows", row, "display.max_info_columns", columns
562+
):
563+
with StringIO() as buf:
564+
df.info(buf=buf, show_counts=show_counts)
565+
assert ("non-null" in buf.getvalue()) is result

pandas/tests/frame/test_repr.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,6 @@ def test_to_records_with_inf_record(self):
422422
result = repr(df)
423423
assert result == expected
424424

425-
def test_masked_ea_with_formatter(self):
426-
# GH#39336
427-
df = DataFrame(
428-
{
429-
"a": Series([0.123456789, 1.123456789], dtype="Float64"),
430-
"b": Series([1, 2], dtype="Int64"),
431-
}
432-
)
433-
result = df.to_string(formatters=["{:.2f}".format, "{:.2f}".format])
434-
expected = """ a b
435-
0 0.12 1.00
436-
1 1.12 2.00"""
437-
assert result == expected
438-
439425
def test_repr_ea_columns(self, any_string_dtype):
440426
# GH#54797
441427
pytest.importorskip("pyarrow")
@@ -446,3 +432,39 @@ def test_repr_ea_columns(self, any_string_dtype):
446432
1 2 5
447433
2 3 6"""
448434
assert repr(df) == expected
435+
436+
437+
@pytest.mark.parametrize(
438+
"data,output",
439+
[
440+
([2, complex("nan"), 1], [" 2.0+0.0j", " NaN+0.0j", " 1.0+0.0j"]),
441+
([2, complex("nan"), -1], [" 2.0+0.0j", " NaN+0.0j", "-1.0+0.0j"]),
442+
([-2, complex("nan"), -1], ["-2.0+0.0j", " NaN+0.0j", "-1.0+0.0j"]),
443+
([-1.23j, complex("nan"), -1], ["-0.00-1.23j", " NaN+0.00j", "-1.00+0.00j"]),
444+
([1.23j, complex("nan"), 1.23], [" 0.00+1.23j", " NaN+0.00j", " 1.23+0.00j"]),
445+
(
446+
[-1.23j, complex(np.nan, np.nan), 1],
447+
["-0.00-1.23j", " NaN+ NaNj", " 1.00+0.00j"],
448+
),
449+
(
450+
[-1.23j, complex(1.2, np.nan), 1],
451+
["-0.00-1.23j", " 1.20+ NaNj", " 1.00+0.00j"],
452+
),
453+
(
454+
[-1.23j, complex(np.nan, -1.2), 1],
455+
["-0.00-1.23j", " NaN-1.20j", " 1.00+0.00j"],
456+
),
457+
],
458+
)
459+
@pytest.mark.parametrize("as_frame", [True, False])
460+
def test_repr_with_complex_nans(data, output, as_frame):
461+
# GH#53762, GH#53841
462+
obj = Series(np.array(data))
463+
if as_frame:
464+
obj = obj.to_frame(name="val")
465+
reprs = [f"{i} {val}" for i, val in enumerate(output)]
466+
expected = f"{'val': >{len(reprs[0])}}\n" + "\n".join(reprs)
467+
else:
468+
reprs = [f"{i} {val}" for i, val in enumerate(output)]
469+
expected = "\n".join(reprs) + "\ndtype: complex128"
470+
assert str(obj) == expected, f"\n{str(obj)}\n\n{expected}"

0 commit comments

Comments
 (0)