Skip to content

Commit e892d46

Browse files
authored
TST: Parameterize/split series/test_repr.py (pandas-dev#45078)
1 parent 543bda9 commit e892d46

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

pandas/tests/series/test_repr.py

+40-25
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_multilevel_name_print(self, lexsorted_two_level_string_multiindex):
4141
expected = "\n".join(expected)
4242
assert repr(ser) == expected
4343

44-
def test_name_printing(self):
44+
def test_small_name_printing(self):
4545
# Test small Series.
4646
s = Series([0, 1, 2])
4747

@@ -51,6 +51,7 @@ def test_name_printing(self):
5151
s.name = None
5252
assert "Name:" not in repr(s)
5353

54+
def test_big_name_printing(self):
5455
# Test big Series (diff code path).
5556
s = Series(range(1000))
5657

@@ -60,32 +61,39 @@ def test_name_printing(self):
6061
s.name = None
6162
assert "Name:" not in repr(s)
6263

64+
def test_empty_name_printing(self):
6365
s = Series(index=date_range("20010101", "20020101"), name="test", dtype=object)
6466
assert "Name: test" in repr(s)
6567

66-
def test_repr(self, datetime_series, string_series, object_series):
67-
str(datetime_series)
68-
str(string_series)
69-
str(string_series.astype(int))
70-
str(object_series)
71-
72-
str(Series(np.random.randn(1000), index=np.arange(1000)))
73-
str(Series(np.random.randn(1000), index=np.arange(1000, 0, step=-1)))
68+
@pytest.mark.parametrize("args", [(), (0, -1)])
69+
def test_float_range(self, args):
70+
str(Series(np.random.randn(1000), index=np.arange(1000, *args)))
7471

72+
def test_empty_object(self):
7573
# empty
7674
str(Series(dtype=object))
7775

76+
def test_string(self, string_series):
77+
str(string_series)
78+
str(string_series.astype(int))
79+
7880
# with NaNs
7981
string_series[5:7] = np.NaN
8082
str(string_series)
8183

84+
def test_object(self, object_series):
85+
str(object_series)
86+
87+
def test_datetime(self, datetime_series):
88+
str(datetime_series)
8289
# with Nones
8390
ots = datetime_series.astype("O")
8491
ots[::2] = None
8592
repr(ots)
8693

87-
# various names
88-
for name in [
94+
@pytest.mark.parametrize(
95+
"name",
96+
[
8997
"",
9098
1,
9199
1.2,
@@ -97,36 +105,43 @@ def test_repr(self, datetime_series, string_series, object_series):
97105
("foo", 1, 2.3),
98106
("\u03B1", "\u03B2", "\u03B3"),
99107
("\u03B1", "bar"),
100-
]:
101-
string_series.name = name
102-
repr(string_series)
108+
],
109+
)
110+
def test_various_names(self, name, string_series):
111+
# various names
112+
string_series.name = name
113+
repr(string_series)
103114

115+
def test_tuple_name(self):
104116
biggie = Series(
105117
np.random.randn(1000), index=np.arange(1000), name=("foo", "bar", "baz")
106118
)
107119
repr(biggie)
108120

109-
# 0 as name
110-
ser = Series(np.random.randn(100), name=0)
111-
rep_str = repr(ser)
112-
assert "Name: 0" in rep_str
113-
121+
@pytest.mark.parametrize("arg", [100, 1001])
122+
def test_tidy_repr_name_0(self, arg):
114123
# tidy repr
115-
ser = Series(np.random.randn(1001), name=0)
124+
ser = Series(np.random.randn(arg), name=0)
116125
rep_str = repr(ser)
117126
assert "Name: 0" in rep_str
118127

128+
def test_newline(self):
119129
ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"])
120130
assert "\t" not in repr(ser)
121131
assert "\r" not in repr(ser)
122132
assert "a\n" not in repr(ser)
123133

134+
@pytest.mark.parametrize(
135+
"name, expected",
136+
[
137+
["foo", "Series([], Name: foo, dtype: int64)"],
138+
[None, "Series([], dtype: int64)"],
139+
],
140+
)
141+
def test_empty_int64(self, name, expected):
124142
# with empty series (#4651)
125-
s = Series([], dtype=np.int64, name="foo")
126-
assert repr(s) == "Series([], Name: foo, dtype: int64)"
127-
128-
s = Series([], dtype=np.int64, name=None)
129-
assert repr(s) == "Series([], dtype: int64)"
143+
s = Series([], dtype=np.int64, name=name)
144+
assert repr(s) == expected
130145

131146
def test_tidy_repr(self):
132147
a = Series(["\u05d0"] * 1000)

pandas/tests/series/test_subclass.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import numpy as np
2+
import pytest
23

34
import pandas as pd
45
import pandas._testing as tm
56

67

78
class TestSeriesSubclassing:
8-
def test_indexing_sliced(self):
9+
@pytest.mark.parametrize(
10+
"idx_method, indexer, exp_data, exp_idx",
11+
[
12+
["loc", ["a", "b"], [1, 2], "ab"],
13+
["iloc", [2, 3], [3, 4], "cd"],
14+
],
15+
)
16+
def test_indexing_sliced(self, idx_method, indexer, exp_data, exp_idx):
917
s = tm.SubclassedSeries([1, 2, 3, 4], index=list("abcd"))
10-
res = s.loc[["a", "b"]]
11-
exp = tm.SubclassedSeries([1, 2], index=list("ab"))
12-
tm.assert_series_equal(res, exp)
13-
14-
res = s.iloc[[2, 3]]
15-
exp = tm.SubclassedSeries([3, 4], index=list("cd"))
16-
tm.assert_series_equal(res, exp)
17-
18-
res = s.loc[["a", "b"]]
19-
exp = tm.SubclassedSeries([1, 2], index=list("ab"))
18+
res = getattr(s, idx_method)[indexer]
19+
exp = tm.SubclassedSeries(exp_data, index=list(exp_idx))
2020
tm.assert_series_equal(res, exp)
2121

2222
def test_to_frame(self):

0 commit comments

Comments
 (0)