Skip to content

Commit 1db2aad

Browse files
authored
TST: Split and parameterize series/test_api.py (pandas-dev#45079)
1 parent f6df343 commit 1db2aad

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

pandas/tests/series/test_api.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ def test_tab_completion(self):
2525
assert "dt" not in dir(s)
2626
assert "cat" not in dir(s)
2727

28+
def test_tab_completion_dt(self):
2829
# similarly for .dt
2930
s = Series(date_range("1/1/2015", periods=5))
3031
assert "dt" in dir(s)
3132
assert "str" not in dir(s)
3233
assert "cat" not in dir(s)
3334

35+
def test_tab_completion_cat(self):
3436
# Similarly for .cat, but with the twist that str and dt should be
3537
# there if the categories are of that type first cat and str.
3638
s = Series(list("abbcd"), dtype="category")
3739
assert "cat" in dir(s)
3840
assert "str" in dir(s) # as it is a string categorical
3941
assert "dt" not in dir(s)
4042

43+
def test_tab_completion_cat_str(self):
4144
# similar to cat and str
4245
s = Series(date_range("1/1/2015", periods=5)).astype("category")
4346
assert "cat" in dir(s)
@@ -60,12 +63,8 @@ def test_tab_completion_with_categorical(self):
6063
"as_unordered",
6164
]
6265

63-
def get_dir(s):
64-
results = [r for r in s.cat.__dir__() if not r.startswith("_")]
65-
return sorted(set(results))
66-
6766
s = Series(list("aabbcde")).astype("category")
68-
results = get_dir(s)
67+
results = sorted({r for r in s.cat.__dir__() if not r.startswith("_")})
6968
tm.assert_almost_equal(results, sorted(set(ok_for_cat)))
7069

7170
@pytest.mark.parametrize(
@@ -98,14 +97,11 @@ def test_index_tab_completion(self, index):
9897
else:
9998
assert x not in dir_s
10099

101-
def test_not_hashable(self):
102-
s_empty = Series(dtype=object)
103-
s = Series([1])
100+
@pytest.mark.parametrize("ser", [Series(dtype=object), Series([1])])
101+
def test_not_hashable(self, ser):
104102
msg = "unhashable type: 'Series'"
105103
with pytest.raises(TypeError, match=msg):
106-
hash(s_empty)
107-
with pytest.raises(TypeError, match=msg):
108-
hash(s)
104+
hash(ser)
109105

110106
def test_contains(self, datetime_series):
111107
tm.assert_contains_all(datetime_series.index, datetime_series)
@@ -138,12 +134,14 @@ def f(x):
138134
expected = tsdf.max()
139135
tm.assert_series_equal(result, expected)
140136

137+
def test_ndarray_compat_like_func(self):
141138
# using an ndarray like function
142139
s = Series(np.random.randn(10))
143140
result = Series(np.ones_like(s))
144141
expected = Series(1, index=range(10), dtype="float64")
145142
tm.assert_series_equal(result, expected)
146143

144+
def test_ndarray_compat_ravel(self):
147145
# ravel
148146
s = Series(np.random.randn(10))
149147
tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F"))
@@ -152,15 +150,15 @@ def test_empty_method(self):
152150
s_empty = Series(dtype=object)
153151
assert s_empty.empty
154152

155-
s2 = Series(index=[1], dtype=object)
156-
for full_series in [Series([1]), s2]:
157-
assert not full_series.empty
153+
@pytest.mark.parametrize("dtype", ["int64", object])
154+
def test_empty_method_full_series(self, dtype):
155+
full_series = Series(index=[1], dtype=dtype)
156+
assert not full_series.empty
158157

159-
def test_integer_series_size(self):
158+
@pytest.mark.parametrize("dtype", [None, "Int64"])
159+
def test_integer_series_size(self, dtype):
160160
# GH 25580
161-
s = Series(range(9))
162-
assert s.size == 9
163-
s = Series(range(9), dtype="Int64")
161+
s = Series(range(9), dtype=dtype)
164162
assert s.size == 9
165163

166164
def test_attrs(self):
@@ -186,19 +184,22 @@ def test_unknown_attribute(self):
186184
with pytest.raises(AttributeError, match=msg):
187185
ser.foo
188186

189-
def test_datetime_series_no_datelike_attrs(self, datetime_series):
187+
@pytest.mark.parametrize("op", ["year", "day", "second", "weekday"])
188+
def test_datetime_series_no_datelike_attrs(self, op, datetime_series):
190189
# GH#7206
191-
for op in ["year", "day", "second", "weekday"]:
192-
msg = f"'Series' object has no attribute '{op}'"
193-
with pytest.raises(AttributeError, match=msg):
194-
getattr(datetime_series, op)
190+
msg = f"'Series' object has no attribute '{op}'"
191+
with pytest.raises(AttributeError, match=msg):
192+
getattr(datetime_series, op)
195193

196194
def test_series_datetimelike_attribute_access(self):
197195
# attribute access should still work!
198196
ser = Series({"year": 2000, "month": 1, "day": 10})
199197
assert ser.year == 2000
200198
assert ser.month == 1
201199
assert ser.day == 10
200+
201+
def test_series_datetimelike_attribute_access_invalid(self):
202+
ser = Series({"year": 2000, "month": 1, "day": 10})
202203
msg = "'Series' object has no attribute 'weekday'"
203204
with pytest.raises(AttributeError, match=msg):
204205
ser.weekday

pandas/tests/series/test_missing.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ def test_isna_for_inf(self):
3636
tm.assert_series_equal(r, e)
3737
tm.assert_series_equal(dr, de)
3838

39-
def test_isnull_for_inf_deprecated(self):
39+
@pytest.mark.parametrize(
40+
"method, expected",
41+
[
42+
["isna", Series([False, True, True, False])],
43+
["dropna", Series(["a", 1.0], index=[0, 3])],
44+
],
45+
)
46+
def test_isnull_for_inf_deprecated(self, method, expected):
4047
# gh-17115
4148
s = Series(["a", np.inf, np.nan, 1.0])
4249
with pd.option_context("mode.use_inf_as_null", True):
43-
r = s.isna()
44-
dr = s.dropna()
45-
46-
e = Series([False, True, True, False])
47-
de = Series(["a", 1.0], index=[0, 3])
48-
tm.assert_series_equal(r, e)
49-
tm.assert_series_equal(dr, de)
50+
result = getattr(s, method)()
51+
tm.assert_series_equal(result, expected)
5052

5153
def test_timedelta64_nan(self):
5254

0 commit comments

Comments
 (0)