Skip to content

TST: Use indices fixture in tests/indexes/test_base.py #32963

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
4 changes: 4 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ def indices(request):
return indices_dict[request.param].copy()


# gh-32974
index = indices


# ----------------------------------------------------------------
# Series'
# ----------------------------------------------------------------
Expand Down
30 changes: 15 additions & 15 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
period_range,
)
import pandas._testing as tm
from pandas.conftest import indices_dict
from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand All @@ -47,18 +46,6 @@
class TestIndex(Base):
_holder = Index

@pytest.fixture
def index(self, request):
"""
Fixture for selectively parametrizing indices_dict via indirect parametrization
(parametrize over indices_dict keys with indirect=True). Defaults to string
index if no keys are provided.
"""
key = getattr(request, "param", "string")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line makes that using index without parametrization is identical to writing @pytest.mark.parametrize("indices", ["string"], indirect=True)

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @SaturnFromTitan for doing this. This was intended to be a follow-up to #28865 #28865 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now replaced by string_index


# copy to avoid mutation, e.g. setting .name
return indices_dict[key].copy()

def create_index(self) -> Index:
return Index(list("abcde"))

Expand All @@ -83,13 +70,15 @@ def test_copy_and_deepcopy(self, index):
def test_constructor_regular(self, indices):
tm.assert_contains_all(indices, indices)

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_constructor_casting(self, index):
# casting
arr = np.array(index)
new_index = Index(arr)
tm.assert_contains_all(arr, new_index)
tm.assert_index_equal(index, new_index)

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_constructor_copy(self, index):
# copy
# index = self.create_index()
Expand Down Expand Up @@ -640,6 +629,7 @@ def test_nanosecond_index_access(self):
expected_ts = np_datetime64_compat("2013-01-01 00:00:00.000000050+0000", "ns")
assert first_value == x[Timestamp(expected_ts)]

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_booleanindex(self, index):
bool_index = np.ones(len(index), dtype=bool)
bool_index[5:30:2] = False
Expand Down Expand Up @@ -681,6 +671,7 @@ def test_empty_fancy_raises(self, index):
with pytest.raises(IndexError, match=msg):
index[empty_farr]

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_intersection(self, index, sort):
first = index[:20]
second = index[:10]
Expand Down Expand Up @@ -712,6 +703,7 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort):
assert result.name == expected.name
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("index", ["string"], indirect=True)
@pytest.mark.parametrize(
"first_name,second_name,expected_name",
[("A", "A", "A"), ("A", "B", None), (None, "B", None)],
Expand Down Expand Up @@ -790,6 +782,7 @@ def test_chained_union(self, sort):
expected = j1.union(j2, sort=sort).union(j3, sort=sort)
tm.assert_index_equal(union, expected)

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_union(self, index, sort):
first = index[5:20]
second = index[:10]
Expand Down Expand Up @@ -827,6 +820,7 @@ def test_union_sort_special_true(self, slice_):
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("klass", [np.array, Series, list])
@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_union_from_iterables(self, index, klass, sort):
# GH 10149
first = index[5:20]
Expand All @@ -839,6 +833,7 @@ def test_union_from_iterables(self, index, klass, sort):
tm.assert_index_equal(result, everything.sort_values())
assert tm.equalContents(result, everything)

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_union_identity(self, index, sort):
first = index[5:20]

Expand Down Expand Up @@ -1009,6 +1004,7 @@ def test_append_empty_preserve_name(self, name, expected):
result = left.append(right)
assert result.name == expected

@pytest.mark.parametrize("index", ["string"], indirect=True)
@pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")])
def test_difference_name_preservation(self, index, second_name, expected, sort):
first = index[5:20]
Expand All @@ -1026,22 +1022,25 @@ def test_difference_name_preservation(self, index, second_name, expected, sort):
else:
assert result.name == expected

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_difference_empty_arg(self, index, sort):
first = index[5:20]
first.name == "name"
first.name = "name"
result = first.difference([], sort)

assert tm.equalContents(result, first)
assert result.name == first.name

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_difference_identity(self, index, sort):
first = index[5:20]
first.name == "name"
first.name = "name"
result = first.difference(first, sort)

assert len(result) == 0
assert result.name == first.name

@pytest.mark.parametrize("index", ["string"], indirect=True)
def test_difference_sort(self, index, sort):
first = index[5:20]
second = index[:10]
Expand Down Expand Up @@ -1867,6 +1866,7 @@ def test_boolean_cmp(self, values):

tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("index", ["string"], indirect=True)
@pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")])
def test_get_level_values(self, index, name, level):
expected = index.copy()
Expand Down