Skip to content

TST: parametrize generic/internals tests (#31900) #44

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 1 commit into from
Feb 12, 2020
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
94 changes: 40 additions & 54 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@ def test_rename_mi(self):
)
df.rename(str.lower)

def test_set_axis_name(self):
@pytest.mark.parametrize("func", ["_set_axis_name", "rename_axis"])
def test_set_axis_name(self, func):
df = pd.DataFrame([[1, 2], [3, 4]])
funcs = ["_set_axis_name", "rename_axis"]
for func in funcs:
result = methodcaller(func, "foo")(df)
assert df.index.name is None
assert result.index.name == "foo"

result = methodcaller(func, "cols", axis=1)(df)
assert df.columns.name is None
assert result.columns.name == "cols"
result = methodcaller(func, "foo")(df)
assert df.index.name is None
assert result.index.name == "foo"

def test_set_axis_name_mi(self):
result = methodcaller(func, "cols", axis=1)(df)
assert df.columns.name is None
assert result.columns.name == "cols"

@pytest.mark.parametrize("func", ["_set_axis_name", "rename_axis"])
def test_set_axis_name_mi(self, func):
df = DataFrame(
np.empty((3, 3)),
index=MultiIndex.from_tuples([("A", x) for x in list("aBc")]),
columns=MultiIndex.from_tuples([("C", x) for x in list("xyz")]),
)

level_names = ["L1", "L2"]
funcs = ["_set_axis_name", "rename_axis"]
for func in funcs:
result = methodcaller(func, level_names)(df)
assert result.index.names == level_names
assert result.columns.names == [None, None]

result = methodcaller(func, level_names, axis=1)(df)
assert result.columns.names == ["L1", "L2"]
assert result.index.names == [None, None]
result = methodcaller(func, level_names)(df)
assert result.index.names == level_names
assert result.columns.names == [None, None]

result = methodcaller(func, level_names, axis=1)(df)
assert result.columns.names == ["L1", "L2"]
assert result.index.names == [None, None]

def test_nonzero_single_element(self):

Expand Down Expand Up @@ -185,36 +185,35 @@ def test_deepcopy_empty(self):

# formerly in Generic but only test DataFrame
class TestDataFrame2:
def test_validate_bool_args(self):
@pytest.mark.parametrize("value", [1, "True", [1, 2, 3], 5.0])
def test_validate_bool_args(self, value):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
invalid_values = [1, "True", [1, 2, 3], 5.0]

for value in invalid_values:
with pytest.raises(ValueError):
super(DataFrame, df).rename_axis(
mapper={"a": "x", "b": "y"}, axis=1, inplace=value
)
with pytest.raises(ValueError):
super(DataFrame, df).rename_axis(
mapper={"a": "x", "b": "y"}, axis=1, inplace=value
)

with pytest.raises(ValueError):
super(DataFrame, df).drop("a", axis=1, inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df).drop("a", axis=1, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._consolidate(inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df)._consolidate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).fillna(value=0, inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df).fillna(value=0, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).replace(to_replace=1, value=7, inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df).replace(to_replace=1, value=7, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).interpolate(inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df).interpolate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._where(cond=df.a > 2, inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df)._where(cond=df.a > 2, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).mask(cond=df.a > 2, inplace=value)
with pytest.raises(ValueError):
super(DataFrame, df).mask(cond=df.a > 2, inplace=value)

def test_unexpected_keyword(self):
# GH8597
Expand Down Expand Up @@ -243,23 +242,10 @@ class TestToXArray:
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
reason="xarray >= 0.10.0 required",
)
@pytest.mark.parametrize(
"index",
[
"FloatIndex",
"IntIndex",
"StringIndex",
"UnicodeIndex",
"DateIndex",
"PeriodIndex",
"CategoricalIndex",
"TimedeltaIndex",
],
)
@pytest.mark.parametrize("index", tm.all_index_generator(3))
def test_to_xarray_index_types(self, index):
from xarray import Dataset

index = getattr(tm, f"make{index}")
df = DataFrame(
{
"a": list("abc"),
Expand All @@ -273,7 +259,7 @@ def test_to_xarray_index_types(self, index):
}
)

df.index = index(3)
df.index = index
df.index.name = "foo"
df.columns.name = "bar"
result = df.to_xarray()
Expand Down
72 changes: 31 additions & 41 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,39 +257,31 @@ def test_metadata_propagation(self):
self.check_metadata(v1 & v2)
self.check_metadata(v1 | v2)

def test_head_tail(self):
@pytest.mark.parametrize("index", tm.all_index_generator(10))
def test_head_tail(self, index):
# GH5370

o = self._construct(shape=10)

# check all index types
for index in [
tm.makeFloatIndex,
tm.makeIntIndex,
tm.makeStringIndex,
tm.makeUnicodeIndex,
tm.makeDateIndex,
tm.makePeriodIndex,
]:
axis = o._get_axis_name(0)
setattr(o, axis, index(len(getattr(o, axis))))
axis = o._get_axis_name(0)
setattr(o, axis, index)

o.head()
o.head()

self._compare(o.head(), o.iloc[:5])
self._compare(o.tail(), o.iloc[-5:])
self._compare(o.head(), o.iloc[:5])
self._compare(o.tail(), o.iloc[-5:])

# 0-len
self._compare(o.head(0), o.iloc[0:0])
self._compare(o.tail(0), o.iloc[0:0])
# 0-len
self._compare(o.head(0), o.iloc[0:0])
self._compare(o.tail(0), o.iloc[0:0])

# bounded
self._compare(o.head(len(o) + 1), o)
self._compare(o.tail(len(o) + 1), o)
# bounded
self._compare(o.head(len(o) + 1), o)
self._compare(o.tail(len(o) + 1), o)

# neg index
self._compare(o.head(-3), o.head(7))
self._compare(o.tail(-3), o.tail(7))
# neg index
self._compare(o.head(-3), o.head(7))
self._compare(o.tail(-3), o.tail(7))

def test_sample(self):
# Fixes issue: 2419
Expand Down Expand Up @@ -468,16 +460,16 @@ def test_stat_unexpected_keyword(self):
with pytest.raises(TypeError, match=errmsg):
obj.any(epic=starwars) # logical_function

def test_api_compat(self):
@pytest.mark.parametrize("func", ["sum", "cumsum", "any", "var"])
def test_api_compat(self, func):

# GH 12021
# compat for __name__, __qualname__

obj = self._construct(5)
for func in ["sum", "cumsum", "any", "var"]:
f = getattr(obj, func)
assert f.__name__ == func
assert f.__qualname__.endswith(func)
f = getattr(obj, func)
assert f.__name__ == func
assert f.__qualname__.endswith(func)

def test_stat_non_defaults_args(self):
obj = self._construct(5)
Expand Down Expand Up @@ -510,19 +502,17 @@ def test_truncate_out_of_bounds(self):
self._compare(big.truncate(before=0, after=3e6), big)
self._compare(big.truncate(before=-1, after=2e6), big)

def test_copy_and_deepcopy(self):
@pytest.mark.parametrize(
"func",
[copy, deepcopy, lambda x: x.copy(deep=False), lambda x: x.copy(deep=True)],
)
@pytest.mark.parametrize("shape", [0, 1, 2])
def test_copy_and_deepcopy(self, shape, func):
# GH 15444
for shape in [0, 1, 2]:
obj = self._construct(shape)
for func in [
copy,
deepcopy,
lambda x: x.copy(deep=False),
lambda x: x.copy(deep=True),
]:
obj_copy = func(obj)
assert obj_copy is not obj
self._compare(obj_copy, obj)
obj = self._construct(shape)
obj_copy = func(obj)
assert obj_copy is not obj
self._compare(obj_copy, obj)

@pytest.mark.parametrize(
"periods,fill_method,limit,exp",
Expand Down
45 changes: 16 additions & 29 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ def test_rename_mi(self):
)
s.rename(str.lower)

def test_set_axis_name(self):
@pytest.mark.parametrize("func", ["rename_axis", "_set_axis_name"])
def test_set_axis_name(self, func):
s = Series([1, 2, 3], index=["a", "b", "c"])
funcs = ["rename_axis", "_set_axis_name"]
name = "foo"
for func in funcs:
result = methodcaller(func, name)(s)
assert s.index.name is None
assert result.index.name == name

def test_set_axis_name_mi(self):
result = methodcaller(func, name)(s)
assert s.index.name is None
assert result.index.name == name

@pytest.mark.parametrize("func", ["rename_axis", "_set_axis_name"])
def test_set_axis_name_mi(self, func):
s = Series(
[11, 21, 31],
index=MultiIndex.from_tuples(
[("A", x) for x in ["a", "B", "c"]], names=["l1", "l2"]
),
)
funcs = ["rename_axis", "_set_axis_name"]
for func in funcs:
result = methodcaller(func, ["L1", "L2"])(s)
assert s.index.name is None
assert s.index.names == ["l1", "l2"]
assert result.index.name is None
assert result.index.names, ["L1", "L2"]

result = methodcaller(func, ["L1", "L2"])(s)
assert s.index.name is None
assert s.index.names == ["l1", "l2"]
assert result.index.name is None
assert result.index.names, ["L1", "L2"]

def test_set_axis_name_raises(self):
s = pd.Series([1])
Expand Down Expand Up @@ -230,24 +230,11 @@ class TestToXArray:
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
reason="xarray >= 0.10.0 required",
)
@pytest.mark.parametrize(
"index",
[
"FloatIndex",
"IntIndex",
"StringIndex",
"UnicodeIndex",
"DateIndex",
"PeriodIndex",
"TimedeltaIndex",
"CategoricalIndex",
],
)
@pytest.mark.parametrize("index", tm.all_index_generator(6))
def test_to_xarray_index_types(self, index):
from xarray import DataArray

index = getattr(tm, f"make{index}")
s = Series(range(6), index=index(6))
s = Series(range(6), index=index)
s.index.name = "foo"
result = s.to_xarray()
repr(result)
Expand Down
Loading