Skip to content

TST: Use multiple instances of parametrize instead of product in tests #21602

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
Jun 23, 2018
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
14 changes: 7 additions & 7 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import re
import pytest

from itertools import product

import numpy as np
import pandas as pd
from pandas import (
Expand Down Expand Up @@ -233,12 +231,14 @@ def test_dst(self):
assert is_datetimetz(s2)
assert s1.dtype == s2.dtype

def test_parser(self):
@pytest.mark.parametrize('tz', ['UTC', 'US/Eastern'])
@pytest.mark.parametrize('constructor', ['M8', 'datetime64'])
def test_parser(self, tz, constructor):
# pr #11245
for tz, constructor in product(('UTC', 'US/Eastern'),
('M8', 'datetime64')):
assert (DatetimeTZDtype('%s[ns, %s]' % (constructor, tz)) ==
DatetimeTZDtype('ns', tz))
dtz_str = '{con}[ns, {tz}]'.format(con=constructor, tz=tz)
result = DatetimeTZDtype(dtz_str)
expected = DatetimeTZDtype('ns', tz)
assert result == expected

def test_empty(self):
dt = DatetimeTZDtype()
Expand Down
124 changes: 63 additions & 61 deletions pandas/tests/frame/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pandas.util.testing import assert_frame_equal
from pandas.tests.frame.common import TestData
from pandas import Series, DataFrame
from pandas.compat import product


class TestRank(TestData):
Expand All @@ -26,6 +25,13 @@ class TestRank(TestData):
'dense': np.array([1, 3, 4, 2, nan, 2, 1, 5, nan, 3]),
}

@pytest.fixture(params=['average', 'min', 'max', 'first', 'dense'])
def method(self, request):
"""
Fixture for trying all rank methods
"""
return request.param

def test_rank(self):
rankdata = pytest.importorskip('scipy.stats.rankdata')

Expand Down Expand Up @@ -217,34 +223,35 @@ def test_rank_methods_frame(self):
expected = expected.astype('float64')
tm.assert_frame_equal(result, expected)

def test_rank_descending(self):
dtypes = ['O', 'f8', 'i8']
@pytest.mark.parametrize('dtype', ['O', 'f8', 'i8'])
def test_rank_descending(self, method, dtype):

for dtype, method in product(dtypes, self.results):
if 'i' in dtype:
df = self.df.dropna()
else:
df = self.df.astype(dtype)
if 'i' in dtype:
df = self.df.dropna()
else:
df = self.df.astype(dtype)

res = df.rank(ascending=False)
expected = (df.max() - df).rank()
assert_frame_equal(res, expected)
res = df.rank(ascending=False)
expected = (df.max() - df).rank()
assert_frame_equal(res, expected)

if method == 'first' and dtype == 'O':
continue
if method == 'first' and dtype == 'O':
return

expected = (df.max() - df).rank(method=method)
expected = (df.max() - df).rank(method=method)

if dtype != 'O':
res2 = df.rank(method=method, ascending=False,
numeric_only=True)
assert_frame_equal(res2, expected)
if dtype != 'O':
res2 = df.rank(method=method, ascending=False,
numeric_only=True)
assert_frame_equal(res2, expected)

res3 = df.rank(method=method, ascending=False,
numeric_only=False)
assert_frame_equal(res3, expected)
res3 = df.rank(method=method, ascending=False,
numeric_only=False)
assert_frame_equal(res3, expected)

def test_rank_2d_tie_methods(self):
@pytest.mark.parametrize('axis', [0, 1])
@pytest.mark.parametrize('dtype', [None, object])
def test_rank_2d_tie_methods(self, method, axis, dtype):
df = self.df

def _check2d(df, expected, method='average', axis=0):
Expand All @@ -257,43 +264,38 @@ def _check2d(df, expected, method='average', axis=0):
result = df.rank(method=method, axis=axis)
assert_frame_equal(result, exp_df)

dtypes = [None, object]
disabled = set([(object, 'first')])
results = self.results

for method, axis, dtype in product(results, [0, 1], dtypes):
if (dtype, method) in disabled:
continue
frame = df if dtype is None else df.astype(dtype)
_check2d(frame, results[method], method=method, axis=axis)


@pytest.mark.parametrize(
"method,exp", [("dense",
[[1., 1., 1.],
[1., 0.5, 2. / 3],
[1., 0.5, 1. / 3]]),
("min",
[[1. / 3, 1., 1.],
[1. / 3, 1. / 3, 2. / 3],
[1. / 3, 1. / 3, 1. / 3]]),
("max",
[[1., 1., 1.],
[1., 2. / 3, 2. / 3],
[1., 2. / 3, 1. / 3]]),
("average",
[[2. / 3, 1., 1.],
[2. / 3, 0.5, 2. / 3],
[2. / 3, 0.5, 1. / 3]]),
("first",
[[1. / 3, 1., 1.],
[2. / 3, 1. / 3, 2. / 3],
[3. / 3, 2. / 3, 1. / 3]])])
def test_rank_pct_true(method, exp):
Copy link
Member Author

Choose a reason for hiding this comment

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

This test was the only one in that was outside the class, which seemed a bit strange, so I moved it inside the class.

# see gh-15630.

df = DataFrame([[2012, 66, 3], [2012, 65, 2], [2012, 65, 1]])
result = df.rank(method=method, pct=True)

expected = DataFrame(exp)
tm.assert_frame_equal(result, expected)
if (dtype, method) in disabled:
return
frame = df if dtype is None else df.astype(dtype)
_check2d(frame, self.results[method], method=method, axis=axis)

@pytest.mark.parametrize(
"method,exp", [("dense",
[[1., 1., 1.],
[1., 0.5, 2. / 3],
[1., 0.5, 1. / 3]]),
("min",
[[1. / 3, 1., 1.],
[1. / 3, 1. / 3, 2. / 3],
[1. / 3, 1. / 3, 1. / 3]]),
("max",
[[1., 1., 1.],
[1., 2. / 3, 2. / 3],
[1., 2. / 3, 1. / 3]]),
("average",
[[2. / 3, 1., 1.],
[2. / 3, 0.5, 2. / 3],
[2. / 3, 0.5, 1. / 3]]),
("first",
[[1. / 3, 1., 1.],
[2. / 3, 1. / 3, 2. / 3],
[3. / 3, 2. / 3, 1. / 3]])])
def test_rank_pct_true(self, method, exp):
# see gh-15630.

df = DataFrame([[2012, 66, 3], [2012, 65, 2], [2012, 65, 1]])
result = df.rank(method=method, pct=True)

expected = DataFrame(exp)
tm.assert_frame_equal(result, expected)
7 changes: 4 additions & 3 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,10 @@ def test_frame_describe_unstacked_format():
# nunique
# --------------------------------

@pytest.mark.parametrize("n, m", cart_product(10 ** np.arange(2, 6),
(10, 100, 1000)))
@pytest.mark.parametrize("sort, dropna", cart_product((False, True), repeat=2))
@pytest.mark.parametrize('n', 10 ** np.arange(2, 6))
@pytest.mark.parametrize('m', [10, 100, 1000])
@pytest.mark.parametrize('sort', [False, True])
@pytest.mark.parametrize('dropna', [False, True])
def test_series_groupby_nunique(n, m, sort, dropna):

def check_nunique(df, keys, as_index=True):
Expand Down
12 changes: 5 additions & 7 deletions pandas/tests/groupby/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numpy as np
from pandas import DataFrame, Series, compat, date_range, Index, MultiIndex
from pandas.util import testing as tm
from pandas.compat import lrange, product

AGG_FUNCTIONS = ['sum', 'prod', 'min', 'max', 'median', 'mean', 'skew',
'mad', 'std', 'var', 'sem']
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a fixture (future PR)

Expand Down Expand Up @@ -175,12 +174,11 @@ def raw_frame():
return raw_frame


@pytest.mark.parametrize(
"op, level, axis, skipna, sort",
product(AGG_FUNCTIONS,
lrange(2), lrange(2),
[True, False],
[True, False]))
@pytest.mark.parametrize('op', AGG_FUNCTIONS)
@pytest.mark.parametrize('level', [0, 1])
@pytest.mark.parametrize('axis', [0, 1])
@pytest.mark.parametrize('skipna', [True, False])
@pytest.mark.parametrize('sort', [True, False])
def test_regression_whitelist_methods(
raw_frame, op, level,
axis, skipna, sort):
Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from warnings import catch_warnings
from itertools import combinations, product
from itertools import combinations

import datetime as dt
import dateutil
Expand Down Expand Up @@ -941,10 +941,11 @@ def test_append_different_columns_types(self, df_columns, series_index):
columns=combined_columns)
assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"index_can_append, index_cannot_append_with_other",
product(indexes_can_append, indexes_cannot_append_with_other),
ids=lambda x: x.__class__.__name__)
@pytest.mark.parametrize('index_can_append', indexes_can_append,
ids=lambda x: x.__class__.__name__)
@pytest.mark.parametrize('index_cannot_append_with_other',
indexes_cannot_append_with_other,
ids=lambda x: x.__class__.__name__)
def test_append_different_columns_types_raises(
self, index_can_append, index_cannot_append_with_other):
# GH18359
Expand Down
20 changes: 9 additions & 11 deletions pandas/tests/sparse/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from pandas.core.sparse.api import SparseSeries
from pandas.tests.series.test_api import SharedWithSparse

from itertools import product


def _test_data1():
# nan-based
Expand Down Expand Up @@ -985,16 +983,16 @@ def test_combine_first(self):
tm.assert_sp_series_equal(result, result2)
tm.assert_sp_series_equal(result, expected)

@pytest.mark.parametrize('deep,fill_values', [([True, False],
[0, 1, np.nan, None])])
def test_memory_usage_deep(self, deep, fill_values):
for deep, fill_value in product(deep, fill_values):
sparse_series = SparseSeries(fill_values, fill_value=fill_value)
dense_series = Series(fill_values)
sparse_usage = sparse_series.memory_usage(deep=deep)
dense_usage = dense_series.memory_usage(deep=deep)
@pytest.mark.parametrize('deep', [True, False])
@pytest.mark.parametrize('fill_value', [0, 1, np.nan, None])
def test_memory_usage_deep(self, deep, fill_value):
values = [0, 1, np.nan, None]
sparse_series = SparseSeries(values, fill_value=fill_value)
dense_series = Series(values)
sparse_usage = sparse_series.memory_usage(deep=deep)
dense_usage = dense_series.memory_usage(deep=deep)

assert sparse_usage < dense_usage
assert sparse_usage < dense_usage


class TestSparseHandlingMultiIndexes(object):
Expand Down
80 changes: 40 additions & 40 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import pandas as pd
import pandas._libs.index as _index

AGG_FUNCTIONS = ['sum', 'prod', 'min', 'max', 'median', 'mean', 'skew', 'mad',
'std', 'var', 'sem']


class Base(object):

Expand Down Expand Up @@ -1389,60 +1392,57 @@ def test_count(self):
pytest.raises(KeyError, series.count, 'x')
pytest.raises(KeyError, frame.count, level='x')

AGG_FUNCTIONS = ['sum', 'prod', 'min', 'max', 'median', 'mean', 'skew',
'mad', 'std', 'var', 'sem']

@pytest.mark.parametrize('op', AGG_FUNCTIONS)
@pytest.mark.parametrize('level', [0, 1])
@pytest.mark.parametrize('skipna', [True, False])
@pytest.mark.parametrize('sort', [True, False])
def test_series_group_min_max(self, sort):
def test_series_group_min_max(self, op, level, skipna, sort):
# GH 17537
for op, level, skipna in cart_product(self.AGG_FUNCTIONS, lrange(2),
[False, True]):
grouped = self.series.groupby(level=level, sort=sort)
aggf = lambda x: getattr(x, op)(skipna=skipna)
# skipna=True
leftside = grouped.agg(aggf)
rightside = getattr(self.series, op)(level=level, skipna=skipna)
if sort:
rightside = rightside.sort_index(level=level)
tm.assert_series_equal(leftside, rightside)

grouped = self.series.groupby(level=level, sort=sort)
# skipna=True
leftside = grouped.agg(lambda x: getattr(x, op)(skipna=skipna))
rightside = getattr(self.series, op)(level=level, skipna=skipna)
if sort:
rightside = rightside.sort_index(level=level)
tm.assert_series_equal(leftside, rightside)

@pytest.mark.parametrize('op', AGG_FUNCTIONS)
@pytest.mark.parametrize('level', [0, 1])
@pytest.mark.parametrize('axis', [0, 1])
@pytest.mark.parametrize('skipna', [True, False])
@pytest.mark.parametrize('sort', [True, False])
def test_frame_group_ops(self, sort):
def test_frame_group_ops(self, op, level, axis, skipna, sort):
# GH 17537
self.frame.iloc[1, [1, 2]] = np.nan
self.frame.iloc[7, [0, 1]] = np.nan

for op, level, axis, skipna in cart_product(self.AGG_FUNCTIONS,
lrange(2), lrange(2),
[False, True]):

if axis == 0:
frame = self.frame
else:
frame = self.frame.T
if axis == 0:
frame = self.frame
else:
frame = self.frame.T

grouped = frame.groupby(level=level, axis=axis, sort=sort)
grouped = frame.groupby(level=level, axis=axis, sort=sort)

pieces = []
pieces = []

def aggf(x):
pieces.append(x)
return getattr(x, op)(skipna=skipna, axis=axis)
def aggf(x):
pieces.append(x)
return getattr(x, op)(skipna=skipna, axis=axis)

leftside = grouped.agg(aggf)
rightside = getattr(frame, op)(level=level, axis=axis,
skipna=skipna)
if sort:
rightside = rightside.sort_index(level=level, axis=axis)
frame = frame.sort_index(level=level, axis=axis)
leftside = grouped.agg(aggf)
rightside = getattr(frame, op)(level=level, axis=axis,
skipna=skipna)
if sort:
rightside = rightside.sort_index(level=level, axis=axis)
frame = frame.sort_index(level=level, axis=axis)

# for good measure, groupby detail
level_index = frame._get_axis(axis).levels[level]
# for good measure, groupby detail
level_index = frame._get_axis(axis).levels[level]

tm.assert_index_equal(leftside._get_axis(axis), level_index)
tm.assert_index_equal(rightside._get_axis(axis), level_index)
tm.assert_index_equal(leftside._get_axis(axis), level_index)
tm.assert_index_equal(rightside._get_axis(axis), level_index)

tm.assert_frame_equal(leftside, rightside)
tm.assert_frame_equal(leftside, rightside)

def test_stat_op_corner(self):
obj = Series([10.0], index=MultiIndex.from_tuples([(2, 3)]))
Expand Down
Loading