Skip to content

CLN: collect ordered fixtures in pandas.conftest #26082

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
Apr 16, 2019
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
6 changes: 6 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def observed(request):
return request.param


@pytest.fixture(params=[True, False, None])
def ordered_fixture(request):
"""Boolean 'ordered' parameter for Categorical."""
return request.param


_all_arithmetic_operators = ['__add__', '__radd__',
'__sub__', '__rsub__',
'__mul__', '__rmul__',
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/arrays/categorical/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@
def allow_fill(request):
"""Boolean 'allow_fill' parameter for Categorical.take"""
return request.param


@pytest.fixture(params=[True, False])
def ordered(request):
"""Boolean 'ordered' parameter for Categorical."""
return request.param
12 changes: 6 additions & 6 deletions pandas/tests/arrays/categorical/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ def test_take_empty(self, allow_fill):
with pytest.raises(IndexError):
cat.take([0], allow_fill=allow_fill)

def test_positional_take(self, ordered):
def test_positional_take(self, ordered_fixture):
cat = pd.Categorical(['a', 'a', 'b', 'b'], categories=['b', 'a'],
ordered=ordered)
ordered=ordered_fixture)
result = cat.take([0, 1, 2], allow_fill=False)
expected = pd.Categorical(['a', 'a', 'b'], categories=cat.categories,
ordered=ordered)
ordered=ordered_fixture)
tm.assert_categorical_equal(result, expected)

def test_positional_take_unobserved(self, ordered):
def test_positional_take_unobserved(self, ordered_fixture):
cat = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'],
ordered=ordered)
ordered=ordered_fixture)
result = cat.take([1, 0], allow_fill=False)
expected = pd.Categorical(['b', 'a'], categories=cat.categories,
ordered=ordered)
ordered=ordered_fixture)
tm.assert_categorical_equal(result, expected)

def test_take_allow_fill(self):
Expand Down
32 changes: 14 additions & 18 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
import pandas.util.testing as tm


@pytest.fixture(params=[True, False, None])
def ordered(request):
return request.param


class Base(object):

def setup_method(self, method):
Expand Down Expand Up @@ -659,10 +654,10 @@ class TestCategoricalDtypeParametrized(object):
['a', 'b', 10, 2, 1.3, True],
[True, False],
pd.date_range('2017', periods=4)])
def test_basic(self, categories, ordered):
c1 = CategoricalDtype(categories, ordered=ordered)
def test_basic(self, categories, ordered_fixture):
c1 = CategoricalDtype(categories, ordered=ordered_fixture)
tm.assert_index_equal(c1.categories, pd.Index(categories))
assert c1.ordered is ordered
assert c1.ordered is ordered_fixture

def test_order_matters(self):
categories = ['a', 'b']
Expand All @@ -683,7 +678,7 @@ def test_categories(self):
tm.assert_index_equal(result.categories, pd.Index(['a', 'b', 'c']))
assert result.ordered is None

def test_equal_but_different(self, ordered):
def test_equal_but_different(self, ordered_fixture):
c1 = CategoricalDtype([1, 2, 3])
c2 = CategoricalDtype([1., 2., 3.])
assert c1 is not c2
Expand Down Expand Up @@ -748,8 +743,9 @@ def test_categorical_equality(self, ordered1, ordered2):

@pytest.mark.parametrize('categories', [list('abc'), None])
@pytest.mark.parametrize('other', ['category', 'not a category'])
def test_categorical_equality_strings(self, categories, ordered, other):
c1 = CategoricalDtype(categories, ordered)
def test_categorical_equality_strings(self, categories, ordered_fixture,
other):
c1 = CategoricalDtype(categories, ordered_fixture)
result = c1 == other
expected = other == 'category'
assert result is expected
Expand Down Expand Up @@ -793,12 +789,12 @@ def test_from_categorical_dtype_both(self):
c1, categories=[1, 2], ordered=False)
assert result == CategoricalDtype([1, 2], ordered=False)

def test_str_vs_repr(self, ordered):
c1 = CategoricalDtype(['a', 'b'], ordered=ordered)
def test_str_vs_repr(self, ordered_fixture):
c1 = CategoricalDtype(['a', 'b'], ordered=ordered_fixture)
assert str(c1) == 'category'
# Py2 will have unicode prefixes
pat = r"CategoricalDtype\(categories=\[.*\], ordered={ordered}\)"
assert re.match(pat.format(ordered=ordered), repr(c1))
assert re.match(pat.format(ordered=ordered_fixture), repr(c1))

def test_categorical_categories(self):
# GH17884
Expand All @@ -810,8 +806,8 @@ def test_categorical_categories(self):
@pytest.mark.parametrize('new_categories', [
list('abc'), list('cba'), list('wxyz'), None])
@pytest.mark.parametrize('new_ordered', [True, False, None])
def test_update_dtype(self, ordered, new_categories, new_ordered):
dtype = CategoricalDtype(list('abc'), ordered)
def test_update_dtype(self, ordered_fixture, new_categories, new_ordered):
dtype = CategoricalDtype(list('abc'), ordered_fixture)
new_dtype = CategoricalDtype(new_categories, new_ordered)

expected_categories = new_dtype.categories
Expand All @@ -826,8 +822,8 @@ def test_update_dtype(self, ordered, new_categories, new_ordered):
tm.assert_index_equal(result.categories, expected_categories)
assert result.ordered is expected_ordered

def test_update_dtype_string(self, ordered):
dtype = CategoricalDtype(list('abc'), ordered)
def test_update_dtype_string(self, ordered_fixture):
dtype = CategoricalDtype(list('abc'), ordered_fixture)
expected_categories = dtype.categories
expected_ordered = dtype.ordered
result = dtype.update_dtype('category')
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,14 +1406,14 @@ def test_value_counts_with_nan(self):
pytest.param("datetime64[D]",
marks=pytest.mark.xfail(reason="GH#7996"))]
)
@pytest.mark.parametrize("is_ordered", [True, False])
def test_drop_duplicates_categorical_non_bool(self, dtype, is_ordered):
def test_drop_duplicates_categorical_non_bool(self, dtype,
ordered_fixture):
cat_array = np.array([1, 2, 3, 4, 5], dtype=np.dtype(dtype))

# Test case 1
input1 = np.array([1, 2, 3, 3], dtype=np.dtype(dtype))
tc1 = Series(Categorical(input1, categories=cat_array,
ordered=is_ordered))
ordered=ordered_fixture))

expected = Series([False, False, False, True])
tm.assert_series_equal(tc1.duplicated(), expected)
Expand All @@ -1440,7 +1440,7 @@ def test_drop_duplicates_categorical_non_bool(self, dtype, is_ordered):
# Test case 2
input2 = np.array([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype(dtype))
tc2 = Series(Categorical(
input2, categories=cat_array, ordered=is_ordered)
input2, categories=cat_array, ordered=ordered_fixture)
)

expected = Series([False, False, False, False, True, True, False])
Expand All @@ -1465,10 +1465,10 @@ def test_drop_duplicates_categorical_non_bool(self, dtype, is_ordered):
sc.drop_duplicates(keep=False, inplace=True)
tm.assert_series_equal(sc, tc2[~expected])

@pytest.mark.parametrize("is_ordered", [True, False])
def test_drop_duplicates_categorical_bool(self, is_ordered):
def test_drop_duplicates_categorical_bool(self, ordered_fixture):
tc = Series(Categorical([True, False, True, False],
categories=[True, False], ordered=is_ordered))
categories=[True, False],
ordered=ordered_fixture))

expected = Series([False, False, True, True])
tm.assert_series_equal(tc.duplicated(), expected)
Expand Down