Skip to content

Commit 03b9ad8

Browse files
authored
TST: parametrize test_info (#37887)
1 parent 3967d83 commit 03b9ad8

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

pandas/tests/io/formats/test_info.py

+53-62
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,13 @@
1616
Series,
1717
date_range,
1818
option_context,
19-
reset_option,
20-
set_option,
2119
)
22-
import pandas._testing as tm
2320

2421

2522
@pytest.fixture
26-
def datetime_frame():
27-
"""
28-
Fixture for DataFrame of floats with DatetimeIndex
29-
30-
Columns are ['A', 'B', 'C', 'D']
31-
32-
A B C D
33-
2000-01-03 -1.122153 0.468535 0.122226 1.693711
34-
2000-01-04 0.189378 0.486100 0.007864 -1.216052
35-
2000-01-05 0.041401 -0.835752 -0.035279 -0.414357
36-
2000-01-06 0.430050 0.894352 0.090719 0.036939
37-
2000-01-07 -0.620982 -0.668211 -0.706153 1.466335
38-
2000-01-10 -0.752633 0.328434 -0.815325 0.699674
39-
2000-01-11 -2.236969 0.615737 -0.829076 -1.196106
40-
... ... ... ... ...
41-
2000-02-03 1.642618 -0.579288 0.046005 1.385249
42-
2000-02-04 -0.544873 -1.160962 -0.284071 -1.418351
43-
2000-02-07 -2.656149 -0.601387 1.410148 0.444150
44-
2000-02-08 -1.201881 -1.289040 0.772992 -1.445300
45-
2000-02-09 1.377373 0.398619 1.008453 -0.928207
46-
2000-02-10 0.473194 -0.636677 0.984058 0.511519
47-
2000-02-11 -0.965556 0.408313 -1.312844 -0.381948
48-
49-
[30 rows x 4 columns]
50-
"""
51-
return DataFrame(tm.getTimeSeriesData())
23+
def duplicate_columns_frame():
24+
"""Dataframe with duplicate column names."""
25+
return DataFrame(np.random.randn(1500, 4), columns=["a", "a", "b", "b"])
5226

5327

5428
def test_info_empty():
@@ -65,9 +39,7 @@ def test_info_empty():
6539
assert result == expected
6640

6741

68-
def test_info_categorical_column():
69-
70-
# make sure it works
42+
def test_info_categorical_column_smoke_test():
7143
n = 2500
7244
df = DataFrame({"int64": np.random.randint(100, size=n)})
7345
df["category"] = Series(
@@ -82,18 +54,48 @@ def test_info_categorical_column():
8254
df2.info(buf=buf)
8355

8456

85-
def test_info(float_frame, datetime_frame):
86-
io = StringIO()
87-
float_frame.info(buf=io)
88-
datetime_frame.info(buf=io)
57+
@pytest.mark.parametrize(
58+
"fixture_func_name",
59+
[
60+
"int_frame",
61+
"float_frame",
62+
"datetime_frame",
63+
"duplicate_columns_frame",
64+
],
65+
)
66+
def test_info_smoke_test(fixture_func_name, request):
67+
frame = request.getfixturevalue(fixture_func_name)
68+
buf = StringIO()
69+
frame.info(buf=buf)
70+
result = buf.getvalue().splitlines()
71+
assert len(result) > 10
8972

90-
frame = DataFrame(np.random.randn(5, 3))
9173

92-
frame.info()
93-
frame.info(verbose=False)
74+
@pytest.mark.parametrize(
75+
"num_columns, max_info_columns, verbose",
76+
[
77+
(10, 100, True),
78+
(10, 11, True),
79+
(10, 10, True),
80+
(10, 9, False),
81+
(10, 1, False),
82+
],
83+
)
84+
def test_info_default_verbose_selection(num_columns, max_info_columns, verbose):
85+
frame = DataFrame(np.random.randn(5, num_columns))
86+
with option_context("display.max_info_columns", max_info_columns):
87+
io_default = StringIO()
88+
frame.info(buf=io_default)
89+
result = io_default.getvalue()
9490

91+
io_explicit = StringIO()
92+
frame.info(buf=io_explicit, verbose=verbose)
93+
expected = io_explicit.getvalue()
9594

96-
def test_info_verbose():
95+
assert result == expected
96+
97+
98+
def test_info_verbose_check_header_separator_body():
9799
buf = StringIO()
98100
size = 1001
99101
start = 5
@@ -202,33 +204,23 @@ def test_info_wide():
202204

203205
io = StringIO()
204206
df.info(buf=io, max_cols=101)
205-
rs = io.getvalue()
206-
assert len(rs.splitlines()) > 100
207-
xp = rs
208-
209-
set_option("display.max_info_columns", 101)
210-
io = StringIO()
211-
df.info(buf=io)
212-
assert rs == xp
213-
reset_option("display.max_info_columns")
214-
207+
result = io.getvalue()
208+
assert len(result.splitlines()) > 100
215209

216-
def test_info_duplicate_columns():
217-
io = StringIO()
218-
219-
# it works!
220-
frame = DataFrame(np.random.randn(1500, 4), columns=["a", "a", "b", "b"])
221-
frame.info(buf=io)
210+
expected = result
211+
with option_context("display.max_info_columns", 101):
212+
io = StringIO()
213+
df.info(buf=io)
214+
result = io.getvalue()
215+
assert result == expected
222216

223217

224218
def test_info_duplicate_columns_shows_correct_dtypes():
225219
# GH11761
226220
io = StringIO()
227-
228221
frame = DataFrame([[1, 2.0]], columns=["a", "a"])
229222
frame.info(buf=io)
230-
io.seek(0)
231-
lines = io.readlines()
223+
lines = io.getvalue().splitlines(True)
232224
assert " 0 a 1 non-null int64 \n" == lines[5]
233225
assert " 1 a 1 non-null float64\n" == lines[6]
234226

@@ -272,7 +264,6 @@ def test_info_max_cols():
272264
assert len(res.strip().split("\n")) == len_
273265

274266
for len_, verbose in [(12, None), (5, False), (12, True)]:
275-
276267
# max_cols not exceeded
277268
with option_context("max_info_columns", 5):
278269
buf = StringIO()
@@ -418,7 +409,6 @@ def test_usage_via_getsizeof():
418409

419410

420411
def test_info_memory_usage_qualified():
421-
422412
buf = StringIO()
423413
df = DataFrame(1, columns=list("ab"), index=[1, 2, 3])
424414
df.info(buf=buf)
@@ -454,7 +444,8 @@ def memory_usage(f):
454444
N = 100
455445
M = len(uppercase)
456446
index = MultiIndex.from_product(
457-
[list(uppercase), date_range("20160101", periods=N)], names=["id", "date"]
447+
[list(uppercase), date_range("20160101", periods=N)],
448+
names=["id", "date"],
458449
)
459450
df = DataFrame({"value": np.random.randn(N * M)}, index=index)
460451

0 commit comments

Comments
 (0)