Skip to content

Commit 2a3e611

Browse files
using string_index fixture instead of @pytest.mark.parametrize(indices, [string], indirect=True)
1 parent 9da6c64 commit 2a3e611

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

pandas/tests/indexes/conftest.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
import pandas._testing as tm
4+
35

46
@pytest.fixture(params=[None, False])
57
def sort(request):
@@ -16,3 +18,9 @@ def sort(request):
1618
in in the Index setops methods.
1719
"""
1820
return request.param
21+
22+
23+
@pytest.fixture
24+
def string_index():
25+
""" Simple string index fixture """
26+
return tm.makeStringIndex(100)

pandas/tests/indexes/test_base.py

+41-52
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,17 @@ def test_copy_and_deepcopy(self, indices):
7070
def test_constructor_regular(self, indices):
7171
tm.assert_contains_all(indices, indices)
7272

73-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
74-
def test_constructor_casting(self, indices):
73+
def test_constructor_casting(self, string_index):
7574
# casting
76-
arr = np.array(indices)
75+
arr = np.array(string_index)
7776
new_index = Index(arr)
7877
tm.assert_contains_all(arr, new_index)
79-
tm.assert_index_equal(indices, new_index)
78+
tm.assert_index_equal(string_index, new_index)
8079

81-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
82-
def test_constructor_copy(self, indices):
80+
def test_constructor_copy(self, string_index):
8381
# copy
8482
# index = self.create_index()
85-
arr = np.array(indices)
83+
arr = np.array(string_index)
8684
new_index = Index(arr, copy=True, name="name")
8785
assert isinstance(new_index, Index)
8886
assert new_index.name == "name"
@@ -629,17 +627,16 @@ def test_nanosecond_index_access(self):
629627
expected_ts = np_datetime64_compat("2013-01-01 00:00:00.000000050+0000", "ns")
630628
assert first_value == x[Timestamp(expected_ts)]
631629

632-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
633-
def test_booleanindex(self, indices):
634-
bool_index = np.ones(len(indices), dtype=bool)
630+
def test_booleanindex(self, string_index):
631+
bool_index = np.ones(len(string_index), dtype=bool)
635632
bool_index[5:30:2] = False
636633

637-
sub_index = indices[bool_index]
634+
sub_index = string_index[bool_index]
638635

639636
for i, val in enumerate(sub_index):
640637
assert sub_index.get_loc(val) == i
641638

642-
sub_index = indices[list(bool_index)]
639+
sub_index = string_index[list(bool_index)]
643640
for i, val in enumerate(sub_index):
644641
assert sub_index.get_loc(val) == i
645642

@@ -671,10 +668,9 @@ def test_empty_fancy_raises(self, indices):
671668
with pytest.raises(IndexError, match=msg):
672669
indices[empty_farr]
673670

674-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
675-
def test_intersection(self, indices, sort):
676-
first = indices[:20]
677-
second = indices[:10]
671+
def test_intersection(self, string_index, sort):
672+
first = string_index[:20]
673+
second = string_index[:10]
678674
intersect = first.intersection(second, sort=sort)
679675
if sort is None:
680676
tm.assert_index_equal(intersect, second.sort_values())
@@ -703,16 +699,15 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort):
703699
assert result.name == expected.name
704700
tm.assert_index_equal(result, expected)
705701

706-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
707702
@pytest.mark.parametrize(
708703
"first_name,second_name,expected_name",
709704
[("A", "A", "A"), ("A", "B", None), (None, "B", None)],
710705
)
711706
def test_intersection_name_preservation2(
712-
self, indices, first_name, second_name, expected_name, sort
707+
self, string_index, first_name, second_name, expected_name, sort
713708
):
714-
first = indices[5:20]
715-
second = indices[:10]
709+
first = string_index[5:20]
710+
second = string_index[:10]
716711
first.name = first_name
717712
second.name = second_name
718713
intersect = first.intersection(second, sort=sort)
@@ -782,11 +777,10 @@ def test_chained_union(self, sort):
782777
expected = j1.union(j2, sort=sort).union(j3, sort=sort)
783778
tm.assert_index_equal(union, expected)
784779

785-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
786-
def test_union(self, indices, sort):
787-
first = indices[5:20]
788-
second = indices[:10]
789-
everything = indices[:20]
780+
def test_union(self, string_index, sort):
781+
first = string_index[5:20]
782+
second = string_index[:10]
783+
everything = string_index[:20]
790784

791785
union = first.union(second, sort=sort)
792786
if sort is None:
@@ -820,22 +814,20 @@ def test_union_sort_special_true(self, slice_):
820814
tm.assert_index_equal(result, expected)
821815

822816
@pytest.mark.parametrize("klass", [np.array, Series, list])
823-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
824-
def test_union_from_iterables(self, indices, klass, sort):
817+
def test_union_from_iterables(self, string_index, klass, sort):
825818
# GH 10149
826-
first = indices[5:20]
827-
second = indices[:10]
828-
everything = indices[:20]
819+
first = string_index[5:20]
820+
second = string_index[:10]
821+
everything = string_index[:20]
829822

830823
case = klass(second.values)
831824
result = first.union(case, sort=sort)
832825
if sort is None:
833826
tm.assert_index_equal(result, everything.sort_values())
834827
assert tm.equalContents(result, everything)
835828

836-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
837-
def test_union_identity(self, indices, sort):
838-
first = indices[5:20]
829+
def test_union_identity(self, string_index, sort):
830+
first = string_index[5:20]
839831

840832
union = first.union(first, sort=sort)
841833
# i.e. identity is not preserved when sort is True
@@ -1004,12 +996,13 @@ def test_append_empty_preserve_name(self, name, expected):
1004996
result = left.append(right)
1005997
assert result.name == expected
1006998

1007-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
1008999
@pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")])
1009-
def test_difference_name_preservation(self, indices, second_name, expected, sort):
1010-
first = indices[5:20]
1011-
second = indices[:10]
1012-
answer = indices[10:20]
1000+
def test_difference_name_preservation(
1001+
self, string_index, second_name, expected, sort
1002+
):
1003+
first = string_index[5:20]
1004+
second = string_index[:10]
1005+
answer = string_index[10:20]
10131006

10141007
first.name = "name"
10151008
second.name = second_name
@@ -1022,31 +1015,28 @@ def test_difference_name_preservation(self, indices, second_name, expected, sort
10221015
else:
10231016
assert result.name == expected
10241017

1025-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
1026-
def test_difference_empty_arg(self, indices, sort):
1027-
first = indices[5:20]
1018+
def test_difference_empty_arg(self, string_index, sort):
1019+
first = string_index[5:20]
10281020
first.name = "name"
10291021
result = first.difference([], sort)
10301022

10311023
assert tm.equalContents(result, first)
10321024
assert result.name == first.name
10331025

1034-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
1035-
def test_difference_identity(self, indices, sort):
1036-
first = indices[5:20]
1026+
def test_difference_identity(self, string_index, sort):
1027+
first = string_index[5:20]
10371028
first.name = "name"
10381029
result = first.difference(first, sort)
10391030

10401031
assert len(result) == 0
10411032
assert result.name == first.name
10421033

1043-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
1044-
def test_difference_sort(self, indices, sort):
1045-
first = indices[5:20]
1046-
second = indices[:10]
1034+
def test_difference_sort(self, string_index, sort):
1035+
first = string_index[5:20]
1036+
second = string_index[:10]
10471037

10481038
result = first.difference(second, sort)
1049-
expected = indices[10:20]
1039+
expected = string_index[10:20]
10501040

10511041
if sort is None:
10521042
expected = expected.sort_values()
@@ -1866,10 +1856,9 @@ def test_boolean_cmp(self, values):
18661856

18671857
tm.assert_numpy_array_equal(result, expected)
18681858

1869-
@pytest.mark.parametrize("indices", ["string"], indirect=True)
18701859
@pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")])
1871-
def test_get_level_values(self, indices, name, level):
1872-
expected = indices.copy()
1860+
def test_get_level_values(self, string_index, name, level):
1861+
expected = string_index.copy()
18731862
if name:
18741863
expected.name = name
18751864

0 commit comments

Comments
 (0)