Skip to content

TST: Split and parameterize series/test_api.py #45079

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 2 commits into from
Dec 27, 2021
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
47 changes: 24 additions & 23 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ def test_tab_completion(self):
assert "dt" not in dir(s)
assert "cat" not in dir(s)

def test_tab_completion_dt(self):
# similarly for .dt
s = Series(date_range("1/1/2015", periods=5))
assert "dt" in dir(s)
assert "str" not in dir(s)
assert "cat" not in dir(s)

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

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

def get_dir(s):
results = [r for r in s.cat.__dir__() if not r.startswith("_")]
return sorted(set(results))

s = Series(list("aabbcde")).astype("category")
results = get_dir(s)
results = sorted({r for r in s.cat.__dir__() if not r.startswith("_")})
tm.assert_almost_equal(results, sorted(set(ok_for_cat)))

@pytest.mark.parametrize(
Expand Down Expand Up @@ -98,14 +97,11 @@ def test_index_tab_completion(self, index):
else:
assert x not in dir_s

def test_not_hashable(self):
s_empty = Series(dtype=object)
s = Series([1])
@pytest.mark.parametrize("ser", [Series(dtype=object), Series([1])])
def test_not_hashable(self, ser):
msg = "unhashable type: 'Series'"
with pytest.raises(TypeError, match=msg):
hash(s_empty)
with pytest.raises(TypeError, match=msg):
hash(s)
hash(ser)

def test_contains(self, datetime_series):
tm.assert_contains_all(datetime_series.index, datetime_series)
Expand Down Expand Up @@ -138,12 +134,14 @@ def f(x):
expected = tsdf.max()
tm.assert_series_equal(result, expected)

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

def test_ndarray_compat_ravel(self):
# ravel
s = Series(np.random.randn(10))
tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F"))
Expand All @@ -152,15 +150,15 @@ def test_empty_method(self):
s_empty = Series(dtype=object)
assert s_empty.empty

s2 = Series(index=[1], dtype=object)
for full_series in [Series([1]), s2]:
assert not full_series.empty
@pytest.mark.parametrize("dtype", ["int64", object])
def test_empty_method_full_series(self, dtype):
full_series = Series(index=[1], dtype=dtype)
assert not full_series.empty

def test_integer_series_size(self):
@pytest.mark.parametrize("dtype", [None, "Int64"])
def test_integer_series_size(self, dtype):
# GH 25580
s = Series(range(9))
assert s.size == 9
s = Series(range(9), dtype="Int64")
s = Series(range(9), dtype=dtype)
assert s.size == 9

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

def test_datetime_series_no_datelike_attrs(self, datetime_series):
@pytest.mark.parametrize("op", ["year", "day", "second", "weekday"])
def test_datetime_series_no_datelike_attrs(self, op, datetime_series):
# GH#7206
for op in ["year", "day", "second", "weekday"]:
msg = f"'Series' object has no attribute '{op}'"
with pytest.raises(AttributeError, match=msg):
getattr(datetime_series, op)
msg = f"'Series' object has no attribute '{op}'"
with pytest.raises(AttributeError, match=msg):
getattr(datetime_series, op)

def test_series_datetimelike_attribute_access(self):
# attribute access should still work!
ser = Series({"year": 2000, "month": 1, "day": 10})
assert ser.year == 2000
assert ser.month == 1
assert ser.day == 10

def test_series_datetimelike_attribute_access_invalid(self):
ser = Series({"year": 2000, "month": 1, "day": 10})
msg = "'Series' object has no attribute 'weekday'"
with pytest.raises(AttributeError, match=msg):
ser.weekday
18 changes: 10 additions & 8 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ def test_isna_for_inf(self):
tm.assert_series_equal(r, e)
tm.assert_series_equal(dr, de)

def test_isnull_for_inf_deprecated(self):
@pytest.mark.parametrize(
"method, expected",
[
["isna", Series([False, True, True, False])],
["dropna", Series(["a", 1.0], index=[0, 3])],
],
)
def test_isnull_for_inf_deprecated(self, method, expected):
# gh-17115
s = Series(["a", np.inf, np.nan, 1.0])
with pd.option_context("mode.use_inf_as_null", True):
r = s.isna()
dr = s.dropna()

e = Series([False, True, True, False])
de = Series(["a", 1.0], index=[0, 3])
tm.assert_series_equal(r, e)
tm.assert_series_equal(dr, de)
result = getattr(s, method)()
tm.assert_series_equal(result, expected)

def test_timedelta64_nan(self):

Expand Down