Skip to content

Commit

Permalink
TST: pytest.mark.parameterize some tests (#44776)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Dec 6, 2021
1 parent 4b71090 commit 5da1b24
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 58 deletions.
70 changes: 32 additions & 38 deletions pandas/tests/test_multilevel.py
Expand Up @@ -48,31 +48,28 @@ def test_reindex_level(self, multiindex_year_month_day_dataframe_random_data):
expected = ymd.groupby(level="month").transform(np.sum).T
tm.assert_frame_equal(result, expected)

def test_binops_level(self, multiindex_year_month_day_dataframe_random_data):
@pytest.mark.parametrize("opname", ["sub", "add", "mul", "div"])
def test_binops_level(
self, opname, multiindex_year_month_day_dataframe_random_data
):
ymd = multiindex_year_month_day_dataframe_random_data

def _check_op(opname):
op = getattr(DataFrame, opname)
with tm.assert_produces_warning(FutureWarning):
month_sums = ymd.sum(level="month")
result = op(ymd, month_sums, level="month")

broadcasted = ymd.groupby(level="month").transform(np.sum)
expected = op(ymd, broadcasted)
tm.assert_frame_equal(result, expected)

# Series
op = getattr(Series, opname)
result = op(ymd["A"], month_sums["A"], level="month")
broadcasted = ymd["A"].groupby(level="month").transform(np.sum)
expected = op(ymd["A"], broadcasted)
expected.name = "A"
tm.assert_series_equal(result, expected)

_check_op("sub")
_check_op("add")
_check_op("mul")
_check_op("div")
op = getattr(DataFrame, opname)
with tm.assert_produces_warning(FutureWarning):
month_sums = ymd.sum(level="month")
result = op(ymd, month_sums, level="month")

broadcasted = ymd.groupby(level="month").transform(np.sum)
expected = op(ymd, broadcasted)
tm.assert_frame_equal(result, expected)

# Series
op = getattr(Series, opname)
result = op(ymd["A"], month_sums["A"], level="month")
broadcasted = ymd["A"].groupby(level="month").transform(np.sum)
expected = op(ymd["A"], broadcasted)
expected.name = "A"
tm.assert_series_equal(result, expected)

def test_reindex(self, multiindex_dataframe_random_data):
frame = multiindex_dataframe_random_data
Expand Down Expand Up @@ -235,25 +232,25 @@ def aggf(x):

tm.assert_frame_equal(leftside, rightside)

def test_std_var_pass_ddof(self):
@pytest.mark.parametrize("meth", ["var", "std"])
def test_std_var_pass_ddof(self, meth):
index = MultiIndex.from_arrays(
[np.arange(5).repeat(10), np.tile(np.arange(10), 5)]
)
df = DataFrame(np.random.randn(len(index), 5), index=index)

for meth in ["var", "std"]:
ddof = 4
alt = lambda x: getattr(x, meth)(ddof=ddof)
ddof = 4
alt = lambda x: getattr(x, meth)(ddof=ddof)

with tm.assert_produces_warning(FutureWarning):
result = getattr(df[0], meth)(level=0, ddof=ddof)
expected = df[0].groupby(level=0).agg(alt)
tm.assert_series_equal(result, expected)
with tm.assert_produces_warning(FutureWarning):
result = getattr(df[0], meth)(level=0, ddof=ddof)
expected = df[0].groupby(level=0).agg(alt)
tm.assert_series_equal(result, expected)

with tm.assert_produces_warning(FutureWarning):
result = getattr(df, meth)(level=0, ddof=ddof)
expected = df.groupby(level=0).agg(alt)
tm.assert_frame_equal(result, expected)
with tm.assert_produces_warning(FutureWarning):
result = getattr(df, meth)(level=0, ddof=ddof)
expected = df.groupby(level=0).agg(alt)
tm.assert_frame_equal(result, expected)

def test_agg_multiple_levels(
self, multiindex_year_month_day_dataframe_random_data, frame_or_series
Expand Down Expand Up @@ -284,9 +281,6 @@ def test_groupby_multilevel(self, multiindex_year_month_day_dataframe_random_dat
result2 = ymd.groupby(level=ymd.index.names[:2]).mean()
tm.assert_frame_equal(result, result2)

def test_groupby_multilevel_with_transform(self):
pass

def test_multilevel_consolidate(self):
index = MultiIndex.from_tuples(
[("foo", "one"), ("foo", "two"), ("bar", "one"), ("bar", "two")]
Expand Down
38 changes: 18 additions & 20 deletions pandas/tests/test_nanops.py
Expand Up @@ -846,7 +846,9 @@ def test_nanvar_ddof(self):
# The overestimated variance.
tm.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var, rtol=1e-2)

def test_ground_truth(self):
@pytest.mark.parametrize("axis", range(2))
@pytest.mark.parametrize("ddof", range(3))
def test_ground_truth(self, axis, ddof):
# Test against values that were precomputed with Numpy.
samples = np.empty((4, 4))
samples[:3, :3] = np.array(
Expand Down Expand Up @@ -875,26 +877,22 @@ def test_ground_truth(self):
)

# Test nanvar.
for axis in range(2):
for ddof in range(3):
var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
tm.assert_almost_equal(var[:3], variance[axis, ddof])
assert np.isnan(var[3])
var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
tm.assert_almost_equal(var[:3], variance[axis, ddof])
assert np.isnan(var[3])

# Test nanstd.
for axis in range(2):
for ddof in range(3):
std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
tm.assert_almost_equal(std[:3], variance[axis, ddof] ** 0.5)
assert np.isnan(std[3])
std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
tm.assert_almost_equal(std[:3], variance[axis, ddof] ** 0.5)
assert np.isnan(std[3])

def test_nanstd_roundoff(self):
@pytest.mark.parametrize("ddof", range(3))
def test_nanstd_roundoff(self, ddof):
# Regression test for GH 10242 (test data taken from GH 10489). Ensure
# that variance is stable.
data = Series(766897346 * np.ones(10))
for ddof in range(3):
result = data.std(ddof=ddof)
assert result == 0.0
result = data.std(ddof=ddof)
assert result == 0.0

@property
def prng(self):
Expand Down Expand Up @@ -959,12 +957,12 @@ def setup_method(self, method):
self.samples = np.sin(np.linspace(0, 1, 200))
self.actual_kurt = -1.2058303433799713

def test_constant_series(self):
@pytest.mark.parametrize("val", [3075.2, 3075.3, 3075.5])
def test_constant_series(self, val):
# xref GH 11974
for val in [3075.2, 3075.3, 3075.5]:
data = val * np.ones(300)
kurt = nanops.nankurt(data)
assert kurt == 0.0
data = val * np.ones(300)
kurt = nanops.nankurt(data)
assert kurt == 0.0

def test_all_finite(self):
alpha, beta = 0.3, 0.1
Expand Down

0 comments on commit 5da1b24

Please sign in to comment.