Skip to content

TST: pytest.mark.parameterize some tests #44776

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
Dec 6, 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
70 changes: 32 additions & 38 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
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"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we parameterize these ops?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we have a fixture with the dunder versions but not the functions versions. Plus I don't think all the fixture versions are supported here.

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
Original file line number Diff line number Diff line change
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