From 6ae9bd22cc1a10d4307b5268d5c0210e84b70797 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 00:05:06 +0100 Subject: [PATCH 1/6] [#30914] Make a code check which bans imports of fixtures in tests From 8ed190db6fa8e0dcfc412ec85f7ea6ce69409b67 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 01:21:43 +0100 Subject: [PATCH 2/6] using indices fixture instead of duplicative index fixture --- pandas/tests/indexes/test_base.py | 260 +++++++++++++++--------------- 1 file changed, 130 insertions(+), 130 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 5bdbc18769ce5..93176474526a7 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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, @@ -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 to avoid mutation, e.g. setting .name - return indices_dict[key].copy() - def create_index(self) -> Index: return Index(list("abcde")) @@ -67,33 +54,35 @@ def test_can_hold_identifiers(self): key = index[0] assert index._can_hold_identifiers_and_holds_name(key) is True - @pytest.mark.parametrize("index", ["datetime"], indirect=True) - def test_new_axis(self, index): + @pytest.mark.parametrize("indices", ["datetime"], indirect=True) + def test_new_axis(self, indices): with tm.assert_produces_warning(DeprecationWarning): # GH#30588 multi-dimensional indexing deprecated - new_index = index[None, :] + new_index = indices[None, :] assert new_index.ndim == 2 assert isinstance(new_index, np.ndarray) - @pytest.mark.parametrize("index", ["int", "uint", "float"], indirect=True) - def test_copy_and_deepcopy(self, index): - new_copy2 = index.copy(dtype=int) + @pytest.mark.parametrize("indices", ["int", "uint", "float"], indirect=True) + def test_copy_and_deepcopy(self, indices): + new_copy2 = indices.copy(dtype=int) assert new_copy2.dtype.kind == "i" def test_constructor_regular(self, indices): tm.assert_contains_all(indices, indices) - def test_constructor_casting(self, index): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_casting(self, indices): # casting - arr = np.array(index) + arr = np.array(indices) new_index = Index(arr) tm.assert_contains_all(arr, new_index) - tm.assert_index_equal(index, new_index) + tm.assert_index_equal(indices, new_index) - def test_constructor_copy(self, index): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_copy(self, indices): # copy # index = self.create_index() - arr = np.array(index) + arr = np.array(indices) new_index = Index(arr, copy=True, name="name") assert isinstance(new_index, Index) assert new_index.name == "name" @@ -498,7 +487,7 @@ def test_constructor_overflow_int64(self): Index([np.iinfo(np.uint64).max - 1], dtype="int64") @pytest.mark.parametrize( - "index", + "indices", [ "datetime", "float", @@ -512,11 +501,11 @@ def test_constructor_overflow_int64(self): ], indirect=True, ) - def test_view_with_args(self, index): - index.view("i8") + def test_view_with_args(self, indices): + indices.view("i8") @pytest.mark.parametrize( - "index", + "indices", [ "unicode", "string", @@ -526,21 +515,21 @@ def test_view_with_args(self, index): ], indirect=True, ) - def test_view_with_args_object_array_raises(self, index): + def test_view_with_args_object_array_raises(self, indices): msg = "Cannot change data-type for object array" with pytest.raises(TypeError, match=msg): - index.view("i8") + indices.view("i8") - @pytest.mark.parametrize("index", ["int", "range"], indirect=True) - def test_astype(self, index): - casted = index.astype("i8") + @pytest.mark.parametrize("indices", ["int", "range"], indirect=True) + def test_astype(self, indices): + casted = indices.astype("i8") # it works! casted.get_loc(5) # pass on name - index.name = "foobar" - casted = index.astype("i8") + indices.name = "foobar" + casted = indices.astype("i8") assert casted.name == "foobar" def test_equals_object(self): @@ -608,17 +597,17 @@ def test_is_(self): ind2 = Index(arr, copy=False) assert not ind1.is_(ind2) - @pytest.mark.parametrize("index", ["datetime"], indirect=True) - def test_asof(self, index): - d = index[0] - assert index.asof(d) == d - assert isna(index.asof(d - timedelta(1))) + @pytest.mark.parametrize("indices", ["datetime"], indirect=True) + def test_asof(self, indices): + d = indices[0] + assert indices.asof(d) == d + assert isna(indices.asof(d - timedelta(1))) - d = index[-1] - assert index.asof(d + timedelta(1)) == d + d = indices[-1] + assert indices.asof(d + timedelta(1)) == d - d = index[0].to_pydatetime() - assert isinstance(index.asof(d), Timestamp) + d = indices[0].to_pydatetime() + assert isinstance(indices.asof(d), Timestamp) def test_asof_datetime_partial(self): index = pd.date_range("2010-01-01", periods=2, freq="m") @@ -640,16 +629,17 @@ 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)] - def test_booleanindex(self, index): - bool_index = np.ones(len(index), dtype=bool) + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_booleanindex(self, indices): + bool_index = np.ones(len(indices), dtype=bool) bool_index[5:30:2] = False - sub_index = index[bool_index] + sub_index = indices[bool_index] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i - sub_index = index[list(bool_index)] + sub_index = indices[list(bool_index)] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i @@ -659,31 +649,32 @@ def test_fancy(self): for i in sl: assert i == sl[sl.get_loc(i)] - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("dtype", [np.int_, np.bool_]) - def test_empty_fancy(self, index, dtype): + def test_empty_fancy(self, indices, dtype): empty_arr = np.array([], dtype=dtype) - empty_index = type(index)([]) + empty_index = type(indices)([]) - assert index[[]].identical(empty_index) - assert index[empty_arr].identical(empty_index) + assert indices[[]].identical(empty_index) + assert indices[empty_arr].identical(empty_index) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_empty_fancy_raises(self, index): + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_empty_fancy_raises(self, indices): # pd.DatetimeIndex is excluded, because it overrides getitem and should # be tested separately. empty_farr = np.array([], dtype=np.float_) - empty_index = type(index)([]) + empty_index = type(indices)([]) - assert index[[]].identical(empty_index) + assert indices[[]].identical(empty_index) # np.ndarray only accepts ndarray of int & bool dtypes, so should Index msg = r"arrays used as indices must be of integer \(or boolean\) type" with pytest.raises(IndexError, match=msg): - index[empty_farr] + indices[empty_farr] - def test_intersection(self, index, sort): - first = index[:20] - second = index[:10] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_intersection(self, indices, sort): + first = indices[:20] + second = indices[:10] intersect = first.intersection(second, sort=sort) if sort is None: tm.assert_index_equal(intersect, second.sort_values()) @@ -712,15 +703,16 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort): assert result.name == expected.name tm.assert_index_equal(result, expected) + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize( "first_name,second_name,expected_name", [("A", "A", "A"), ("A", "B", None), (None, "B", None)], ) def test_intersection_name_preservation2( - self, index, first_name, second_name, expected_name, sort + self, indices, first_name, second_name, expected_name, sort ): - first = index[5:20] - second = index[:10] + first = indices[5:20] + second = indices[:10] first.name = first_name second.name = second_name intersect = first.intersection(second, sort=sort) @@ -790,10 +782,11 @@ def test_chained_union(self, sort): expected = j1.union(j2, sort=sort).union(j3, sort=sort) tm.assert_index_equal(union, expected) - def test_union(self, index, sort): - first = index[5:20] - second = index[:10] - everything = index[:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union(self, indices, sort): + first = indices[5:20] + second = indices[:10] + everything = indices[:20] union = first.union(second, sort=sort) if sort is None: @@ -827,11 +820,12 @@ def test_union_sort_special_true(self, slice_): tm.assert_index_equal(result, expected) @pytest.mark.parametrize("klass", [np.array, Series, list]) - def test_union_from_iterables(self, index, klass, sort): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_from_iterables(self, indices, klass, sort): # GH 10149 - first = index[5:20] - second = index[:10] - everything = index[:20] + first = indices[5:20] + second = indices[:10] + everything = indices[:20] case = klass(second.values) result = first.union(case, sort=sort) @@ -839,8 +833,9 @@ def test_union_from_iterables(self, index, klass, sort): tm.assert_index_equal(result, everything.sort_values()) assert tm.equalContents(result, everything) - def test_union_identity(self, index, sort): - first = index[5:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_identity(self, indices, sort): + first = indices[5:20] union = first.union(first, sort=sort) # i.e. identity is not preserved when sort is True @@ -1009,11 +1004,12 @@ def test_append_empty_preserve_name(self, name, expected): result = left.append(right) assert result.name == expected + @pytest.mark.parametrize("indices", ["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] - second = index[:10] - answer = index[10:20] + def test_difference_name_preservation(self, indices, second_name, expected, sort): + first = indices[5:20] + second = indices[:10] + answer = indices[10:20] first.name = "name" second.name = second_name @@ -1026,28 +1022,31 @@ def test_difference_name_preservation(self, index, second_name, expected, sort): else: assert result.name == expected - def test_difference_empty_arg(self, index, sort): - first = index[5:20] - first.name == "name" + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_empty_arg(self, indices, sort): + first = indices[5:20] + first.name = "name" result = first.difference([], sort) assert tm.equalContents(result, first) assert result.name == first.name - def test_difference_identity(self, index, sort): - first = index[5:20] - first.name == "name" + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_identity(self, indices, sort): + first = indices[5:20] + first.name = "name" result = first.difference(first, sort) assert len(result) == 0 assert result.name == first.name - def test_difference_sort(self, index, sort): - first = index[5:20] - second = index[:10] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_sort(self, indices, sort): + first = indices[5:20] + second = indices[:10] result = first.difference(second, sort) - expected = index[10:20] + expected = indices[10:20] if sort is None: expected = expected.sort_values() @@ -1162,7 +1161,7 @@ def test_intersection_difference(self, indices, sort): tm.assert_index_equal(inter, diff) @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", False), ("bool", False), @@ -1171,13 +1170,13 @@ def test_intersection_difference(self, indices, sort): ("datetime", False), ("float", True), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_numeric(self, index, expected): - assert index.is_numeric() is expected + def test_is_numeric(self, indices, expected): + assert indices.is_numeric() is expected @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", True), ("bool", True), @@ -1186,13 +1185,13 @@ def test_is_numeric(self, index, expected): ("datetime", False), ("float", False), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_object(self, index, expected): - assert index.is_object() is expected + def test_is_object(self, indices, expected): + assert indices.is_object() is expected @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", False), ("bool", False), @@ -1201,10 +1200,10 @@ def test_is_object(self, index, expected): ("datetime", True), ("float", False), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_all_dates(self, index, expected): - assert index.is_all_dates is expected + def test_is_all_dates(self, indices, expected): + assert indices.is_all_dates is expected def test_summary(self, indices): self._check_method_works(Index._summary, indices) @@ -1584,37 +1583,37 @@ def test_slice_locs_negative_step(self, in_slice, expected): expected = pd.Index(list(expected)) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label(self, index): - n = len(index) - drop = index[list(range(5, 10))] - dropped = index.drop(drop) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label(self, indices): + n = len(indices) + drop = indices[list(range(5, 10))] + dropped = indices.drop(drop) - expected = index[list(range(5)) + list(range(10, n))] + expected = indices[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = index.drop(index[0]) - expected = index[1:] + dropped = indices.drop(indices[0]) + expected = indices[1:] tm.assert_index_equal(dropped, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]]) - def test_drop_by_str_label_raises_missing_keys(self, index, keys): + def test_drop_by_str_label_raises_missing_keys(self, indices, keys): with pytest.raises(KeyError, match=""): - index.drop(keys) + indices.drop(keys) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label_errors_ignore(self, index): - n = len(index) - drop = index[list(range(5, 10))] + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label_errors_ignore(self, indices): + n = len(indices) + drop = indices[list(range(5, 10))] mixed = drop.tolist() + ["foo"] - dropped = index.drop(mixed, errors="ignore") + dropped = indices.drop(mixed, errors="ignore") - expected = index[list(range(5)) + list(range(10, n))] + expected = indices[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = index.drop(["foo", "bar"], errors="ignore") - expected = index[list(range(n))] + dropped = indices.drop(["foo", "bar"], errors="ignore") + expected = indices[list(range(n))] tm.assert_index_equal(dropped, expected) def test_drop_by_numeric_label_loc(self): @@ -1734,18 +1733,18 @@ def test_set_value_deprecated(self): assert arr[1] == 80 @pytest.mark.parametrize( - "index", ["string", "int", "datetime", "timedelta"], indirect=True + "indices", ["string", "int", "datetime", "timedelta"], indirect=True ) - def test_get_value(self, index): + def test_get_value(self, indices): # TODO: Remove function? GH 19728 values = np.random.randn(100) - value = index[67] + value = indices[67] with pytest.raises(AttributeError, match="has no attribute '_values'"): # Index.get_value requires a Series, not an ndarray - index.get_value(values, value) + indices.get_value(values, value) - result = index.get_value(Series(values, index=values), value) + result = indices.get_value(Series(values, index=values), value) tm.assert_almost_equal(result, values[67]) @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}]) @@ -1867,9 +1866,10 @@ def test_boolean_cmp(self, values): tm.assert_numpy_array_equal(result, expected) + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")]) - def test_get_level_values(self, index, name, level): - expected = index.copy() + def test_get_level_values(self, indices, name, level): + expected = indices.copy() if name: expected.name = name @@ -1881,13 +1881,13 @@ def test_slice_keep_name(self): assert index.name == index[1:].name @pytest.mark.parametrize( - "index", + "indices", ["unicode", "string", "datetime", "int", "uint", "float"], indirect=True, ) - def test_join_self(self, index, join_type): - joined = index.join(index, how=join_type) - assert index is joined + def test_join_self(self, indices, join_type): + joined = indices.join(indices, how=join_type) + assert indices is joined @pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"]) def test_str_attribute(self, method): From 1eba26df7e55c353eb0ffc68e00e33ccd4dfc403 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 13:09:11 +0100 Subject: [PATCH 3/6] calling 'indices' fixture as 'index' --- pandas/conftest.py | 4 + pandas/tests/indexes/test_base.py | 256 +++++++++++++++--------------- 2 files changed, 132 insertions(+), 128 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 903e1a5dec132..61941624e2e3f 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -404,6 +404,10 @@ def indices(request): return indices_dict[request.param].copy() +# gh-32974 +index = indices + + # ---------------------------------------------------------------- # Series' # ---------------------------------------------------------------- diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 93176474526a7..8bda1fffcfe46 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -54,35 +54,35 @@ def test_can_hold_identifiers(self): key = index[0] assert index._can_hold_identifiers_and_holds_name(key) is True - @pytest.mark.parametrize("indices", ["datetime"], indirect=True) - def test_new_axis(self, indices): + @pytest.mark.parametrize("index", ["datetime"], indirect=True) + def test_new_axis(self, index): with tm.assert_produces_warning(DeprecationWarning): # GH#30588 multi-dimensional indexing deprecated - new_index = indices[None, :] + new_index = index[None, :] assert new_index.ndim == 2 assert isinstance(new_index, np.ndarray) - @pytest.mark.parametrize("indices", ["int", "uint", "float"], indirect=True) - def test_copy_and_deepcopy(self, indices): - new_copy2 = indices.copy(dtype=int) + @pytest.mark.parametrize("index", ["int", "uint", "float"], indirect=True) + def test_copy_and_deepcopy(self, index): + new_copy2 = index.copy(dtype=int) assert new_copy2.dtype.kind == "i" def test_constructor_regular(self, indices): tm.assert_contains_all(indices, indices) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_constructor_casting(self, indices): + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_constructor_casting(self, index): # casting - arr = np.array(indices) + arr = np.array(index) new_index = Index(arr) tm.assert_contains_all(arr, new_index) - tm.assert_index_equal(indices, new_index) + tm.assert_index_equal(index, new_index) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_constructor_copy(self, indices): + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_constructor_copy(self, index): # copy # index = self.create_index() - arr = np.array(indices) + arr = np.array(index) new_index = Index(arr, copy=True, name="name") assert isinstance(new_index, Index) assert new_index.name == "name" @@ -487,7 +487,7 @@ def test_constructor_overflow_int64(self): Index([np.iinfo(np.uint64).max - 1], dtype="int64") @pytest.mark.parametrize( - "indices", + "index", [ "datetime", "float", @@ -501,11 +501,11 @@ def test_constructor_overflow_int64(self): ], indirect=True, ) - def test_view_with_args(self, indices): - indices.view("i8") + def test_view_with_args(self, index): + index.view("i8") @pytest.mark.parametrize( - "indices", + "index", [ "unicode", "string", @@ -515,21 +515,21 @@ def test_view_with_args(self, indices): ], indirect=True, ) - def test_view_with_args_object_array_raises(self, indices): + def test_view_with_args_object_array_raises(self, index): msg = "Cannot change data-type for object array" with pytest.raises(TypeError, match=msg): - indices.view("i8") + index.view("i8") - @pytest.mark.parametrize("indices", ["int", "range"], indirect=True) - def test_astype(self, indices): - casted = indices.astype("i8") + @pytest.mark.parametrize("index", ["int", "range"], indirect=True) + def test_astype(self, index): + casted = index.astype("i8") # it works! casted.get_loc(5) # pass on name - indices.name = "foobar" - casted = indices.astype("i8") + index.name = "foobar" + casted = index.astype("i8") assert casted.name == "foobar" def test_equals_object(self): @@ -597,17 +597,17 @@ def test_is_(self): ind2 = Index(arr, copy=False) assert not ind1.is_(ind2) - @pytest.mark.parametrize("indices", ["datetime"], indirect=True) - def test_asof(self, indices): - d = indices[0] - assert indices.asof(d) == d - assert isna(indices.asof(d - timedelta(1))) + @pytest.mark.parametrize("index", ["datetime"], indirect=True) + def test_asof(self, index): + d = index[0] + assert index.asof(d) == d + assert isna(index.asof(d - timedelta(1))) - d = indices[-1] - assert indices.asof(d + timedelta(1)) == d + d = index[-1] + assert index.asof(d + timedelta(1)) == d - d = indices[0].to_pydatetime() - assert isinstance(indices.asof(d), Timestamp) + d = index[0].to_pydatetime() + assert isinstance(index.asof(d), Timestamp) def test_asof_datetime_partial(self): index = pd.date_range("2010-01-01", periods=2, freq="m") @@ -629,17 +629,17 @@ 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("indices", ["string"], indirect=True) - def test_booleanindex(self, indices): - bool_index = np.ones(len(indices), dtype=bool) + @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 - sub_index = indices[bool_index] + sub_index = index[bool_index] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i - sub_index = indices[list(bool_index)] + sub_index = index[list(bool_index)] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i @@ -649,32 +649,32 @@ def test_fancy(self): for i in sl: assert i == sl[sl.get_loc(i)] - @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("dtype", [np.int_, np.bool_]) - def test_empty_fancy(self, indices, dtype): + def test_empty_fancy(self, index, dtype): empty_arr = np.array([], dtype=dtype) - empty_index = type(indices)([]) + empty_index = type(index)([]) - assert indices[[]].identical(empty_index) - assert indices[empty_arr].identical(empty_index) + assert index[[]].identical(empty_index) + assert index[empty_arr].identical(empty_index) - @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) - def test_empty_fancy_raises(self, indices): + @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + def test_empty_fancy_raises(self, index): # pd.DatetimeIndex is excluded, because it overrides getitem and should # be tested separately. empty_farr = np.array([], dtype=np.float_) - empty_index = type(indices)([]) + empty_index = type(index)([]) - assert indices[[]].identical(empty_index) + assert index[[]].identical(empty_index) # np.ndarray only accepts ndarray of int & bool dtypes, so should Index msg = r"arrays used as indices must be of integer \(or boolean\) type" with pytest.raises(IndexError, match=msg): - indices[empty_farr] + index[empty_farr] - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_intersection(self, indices, sort): - first = indices[:20] - second = indices[:10] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_intersection(self, index, sort): + first = index[:20] + second = index[:10] intersect = first.intersection(second, sort=sort) if sort is None: tm.assert_index_equal(intersect, second.sort_values()) @@ -703,16 +703,16 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort): assert result.name == expected.name tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("indices", ["string"], indirect=True) + @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)], ) def test_intersection_name_preservation2( - self, indices, first_name, second_name, expected_name, sort + self, index, first_name, second_name, expected_name, sort ): - first = indices[5:20] - second = indices[:10] + first = index[5:20] + second = index[:10] first.name = first_name second.name = second_name intersect = first.intersection(second, sort=sort) @@ -782,11 +782,11 @@ 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("indices", ["string"], indirect=True) - def test_union(self, indices, sort): - first = indices[5:20] - second = indices[:10] - everything = indices[:20] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_union(self, index, sort): + first = index[5:20] + second = index[:10] + everything = index[:20] union = first.union(second, sort=sort) if sort is None: @@ -820,12 +820,12 @@ 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("indices", ["string"], indirect=True) - def test_union_from_iterables(self, indices, klass, sort): + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_union_from_iterables(self, index, klass, sort): # GH 10149 - first = indices[5:20] - second = indices[:10] - everything = indices[:20] + first = index[5:20] + second = index[:10] + everything = index[:20] case = klass(second.values) result = first.union(case, sort=sort) @@ -833,9 +833,9 @@ def test_union_from_iterables(self, indices, klass, sort): tm.assert_index_equal(result, everything.sort_values()) assert tm.equalContents(result, everything) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_union_identity(self, indices, sort): - first = indices[5:20] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_union_identity(self, index, sort): + first = index[5:20] union = first.union(first, sort=sort) # i.e. identity is not preserved when sort is True @@ -1004,12 +1004,12 @@ def test_append_empty_preserve_name(self, name, expected): result = left.append(right) assert result.name == expected - @pytest.mark.parametrize("indices", ["string"], indirect=True) + @pytest.mark.parametrize("index", ["string"], indirect=True) @pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")]) - def test_difference_name_preservation(self, indices, second_name, expected, sort): - first = indices[5:20] - second = indices[:10] - answer = indices[10:20] + def test_difference_name_preservation(self, index, second_name, expected, sort): + first = index[5:20] + second = index[:10] + answer = index[10:20] first.name = "name" second.name = second_name @@ -1022,31 +1022,31 @@ def test_difference_name_preservation(self, indices, second_name, expected, sort else: assert result.name == expected - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_empty_arg(self, indices, sort): - first = indices[5:20] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_difference_empty_arg(self, index, sort): + first = index[5:20] first.name = "name" result = first.difference([], sort) assert tm.equalContents(result, first) assert result.name == first.name - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_identity(self, indices, sort): - first = indices[5:20] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_difference_identity(self, index, sort): + first = index[5:20] first.name = "name" result = first.difference(first, sort) assert len(result) == 0 assert result.name == first.name - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_sort(self, indices, sort): - first = indices[5:20] - second = indices[:10] + @pytest.mark.parametrize("index", ["string"], indirect=True) + def test_difference_sort(self, index, sort): + first = index[5:20] + second = index[:10] result = first.difference(second, sort) - expected = indices[10:20] + expected = index[10:20] if sort is None: expected = expected.sort_values() @@ -1161,7 +1161,7 @@ def test_intersection_difference(self, indices, sort): tm.assert_index_equal(inter, diff) @pytest.mark.parametrize( - "indices, expected", + "index, expected", [ ("string", False), ("bool", False), @@ -1170,13 +1170,13 @@ def test_intersection_difference(self, indices, sort): ("datetime", False), ("float", True), ], - indirect=["indices"], + indirect=["index"], ) - def test_is_numeric(self, indices, expected): - assert indices.is_numeric() is expected + def test_is_numeric(self, index, expected): + assert index.is_numeric() is expected @pytest.mark.parametrize( - "indices, expected", + "index, expected", [ ("string", True), ("bool", True), @@ -1185,13 +1185,13 @@ def test_is_numeric(self, indices, expected): ("datetime", False), ("float", False), ], - indirect=["indices"], + indirect=["index"], ) - def test_is_object(self, indices, expected): - assert indices.is_object() is expected + def test_is_object(self, index, expected): + assert index.is_object() is expected @pytest.mark.parametrize( - "indices, expected", + "index, expected", [ ("string", False), ("bool", False), @@ -1200,10 +1200,10 @@ def test_is_object(self, indices, expected): ("datetime", True), ("float", False), ], - indirect=["indices"], + indirect=["index"], ) - def test_is_all_dates(self, indices, expected): - assert indices.is_all_dates is expected + def test_is_all_dates(self, index, expected): + assert index.is_all_dates is expected def test_summary(self, indices): self._check_method_works(Index._summary, indices) @@ -1583,37 +1583,37 @@ def test_slice_locs_negative_step(self, in_slice, expected): expected = pd.Index(list(expected)) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label(self, indices): - n = len(indices) - drop = indices[list(range(5, 10))] - dropped = indices.drop(drop) + @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label(self, index): + n = len(index) + drop = index[list(range(5, 10))] + dropped = index.drop(drop) - expected = indices[list(range(5)) + list(range(10, n))] + expected = index[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = indices.drop(indices[0]) - expected = indices[1:] + dropped = index.drop(index[0]) + expected = index[1:] tm.assert_index_equal(dropped, expected) - @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]]) - def test_drop_by_str_label_raises_missing_keys(self, indices, keys): + def test_drop_by_str_label_raises_missing_keys(self, index, keys): with pytest.raises(KeyError, match=""): - indices.drop(keys) + index.drop(keys) - @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label_errors_ignore(self, indices): - n = len(indices) - drop = indices[list(range(5, 10))] + @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label_errors_ignore(self, index): + n = len(index) + drop = index[list(range(5, 10))] mixed = drop.tolist() + ["foo"] - dropped = indices.drop(mixed, errors="ignore") + dropped = index.drop(mixed, errors="ignore") - expected = indices[list(range(5)) + list(range(10, n))] + expected = index[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = indices.drop(["foo", "bar"], errors="ignore") - expected = indices[list(range(n))] + dropped = index.drop(["foo", "bar"], errors="ignore") + expected = index[list(range(n))] tm.assert_index_equal(dropped, expected) def test_drop_by_numeric_label_loc(self): @@ -1733,18 +1733,18 @@ def test_set_value_deprecated(self): assert arr[1] == 80 @pytest.mark.parametrize( - "indices", ["string", "int", "datetime", "timedelta"], indirect=True + "index", ["string", "int", "datetime", "timedelta"], indirect=True ) - def test_get_value(self, indices): + def test_get_value(self, index): # TODO: Remove function? GH 19728 values = np.random.randn(100) - value = indices[67] + value = index[67] with pytest.raises(AttributeError, match="has no attribute '_values'"): # Index.get_value requires a Series, not an ndarray - indices.get_value(values, value) + index.get_value(values, value) - result = indices.get_value(Series(values, index=values), value) + result = index.get_value(Series(values, index=values), value) tm.assert_almost_equal(result, values[67]) @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}]) @@ -1866,10 +1866,10 @@ def test_boolean_cmp(self, values): tm.assert_numpy_array_equal(result, expected) - @pytest.mark.parametrize("indices", ["string"], indirect=True) + @pytest.mark.parametrize("index", ["string"], indirect=True) @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")]) - def test_get_level_values(self, indices, name, level): - expected = indices.copy() + def test_get_level_values(self, index, name, level): + expected = index.copy() if name: expected.name = name @@ -1881,13 +1881,13 @@ def test_slice_keep_name(self): assert index.name == index[1:].name @pytest.mark.parametrize( - "indices", + "index", ["unicode", "string", "datetime", "int", "uint", "float"], indirect=True, ) - def test_join_self(self, indices, join_type): - joined = indices.join(indices, how=join_type) - assert indices is joined + def test_join_self(self, index, join_type): + joined = index.join(index, how=join_type) + assert index is joined @pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"]) def test_str_attribute(self, method): From 9da6c641944523ff7394540aa5a38384105a9988 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 21:27:53 +0100 Subject: [PATCH 4/6] Revert "calling 'indices' fixture as 'index'" This reverts commit 1eba26df7e55c353eb0ffc68e00e33ccd4dfc403. --- pandas/conftest.py | 4 - pandas/tests/indexes/test_base.py | 256 +++++++++++++++--------------- 2 files changed, 128 insertions(+), 132 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 61941624e2e3f..903e1a5dec132 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -404,10 +404,6 @@ def indices(request): return indices_dict[request.param].copy() -# gh-32974 -index = indices - - # ---------------------------------------------------------------- # Series' # ---------------------------------------------------------------- diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 8bda1fffcfe46..93176474526a7 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -54,35 +54,35 @@ def test_can_hold_identifiers(self): key = index[0] assert index._can_hold_identifiers_and_holds_name(key) is True - @pytest.mark.parametrize("index", ["datetime"], indirect=True) - def test_new_axis(self, index): + @pytest.mark.parametrize("indices", ["datetime"], indirect=True) + def test_new_axis(self, indices): with tm.assert_produces_warning(DeprecationWarning): # GH#30588 multi-dimensional indexing deprecated - new_index = index[None, :] + new_index = indices[None, :] assert new_index.ndim == 2 assert isinstance(new_index, np.ndarray) - @pytest.mark.parametrize("index", ["int", "uint", "float"], indirect=True) - def test_copy_and_deepcopy(self, index): - new_copy2 = index.copy(dtype=int) + @pytest.mark.parametrize("indices", ["int", "uint", "float"], indirect=True) + def test_copy_and_deepcopy(self, indices): + new_copy2 = indices.copy(dtype=int) assert new_copy2.dtype.kind == "i" 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): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_casting(self, indices): # casting - arr = np.array(index) + arr = np.array(indices) new_index = Index(arr) tm.assert_contains_all(arr, new_index) - tm.assert_index_equal(index, new_index) + tm.assert_index_equal(indices, new_index) - @pytest.mark.parametrize("index", ["string"], indirect=True) - def test_constructor_copy(self, index): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_copy(self, indices): # copy # index = self.create_index() - arr = np.array(index) + arr = np.array(indices) new_index = Index(arr, copy=True, name="name") assert isinstance(new_index, Index) assert new_index.name == "name" @@ -487,7 +487,7 @@ def test_constructor_overflow_int64(self): Index([np.iinfo(np.uint64).max - 1], dtype="int64") @pytest.mark.parametrize( - "index", + "indices", [ "datetime", "float", @@ -501,11 +501,11 @@ def test_constructor_overflow_int64(self): ], indirect=True, ) - def test_view_with_args(self, index): - index.view("i8") + def test_view_with_args(self, indices): + indices.view("i8") @pytest.mark.parametrize( - "index", + "indices", [ "unicode", "string", @@ -515,21 +515,21 @@ def test_view_with_args(self, index): ], indirect=True, ) - def test_view_with_args_object_array_raises(self, index): + def test_view_with_args_object_array_raises(self, indices): msg = "Cannot change data-type for object array" with pytest.raises(TypeError, match=msg): - index.view("i8") + indices.view("i8") - @pytest.mark.parametrize("index", ["int", "range"], indirect=True) - def test_astype(self, index): - casted = index.astype("i8") + @pytest.mark.parametrize("indices", ["int", "range"], indirect=True) + def test_astype(self, indices): + casted = indices.astype("i8") # it works! casted.get_loc(5) # pass on name - index.name = "foobar" - casted = index.astype("i8") + indices.name = "foobar" + casted = indices.astype("i8") assert casted.name == "foobar" def test_equals_object(self): @@ -597,17 +597,17 @@ def test_is_(self): ind2 = Index(arr, copy=False) assert not ind1.is_(ind2) - @pytest.mark.parametrize("index", ["datetime"], indirect=True) - def test_asof(self, index): - d = index[0] - assert index.asof(d) == d - assert isna(index.asof(d - timedelta(1))) + @pytest.mark.parametrize("indices", ["datetime"], indirect=True) + def test_asof(self, indices): + d = indices[0] + assert indices.asof(d) == d + assert isna(indices.asof(d - timedelta(1))) - d = index[-1] - assert index.asof(d + timedelta(1)) == d + d = indices[-1] + assert indices.asof(d + timedelta(1)) == d - d = index[0].to_pydatetime() - assert isinstance(index.asof(d), Timestamp) + d = indices[0].to_pydatetime() + assert isinstance(indices.asof(d), Timestamp) def test_asof_datetime_partial(self): index = pd.date_range("2010-01-01", periods=2, freq="m") @@ -629,17 +629,17 @@ 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) + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_booleanindex(self, indices): + bool_index = np.ones(len(indices), dtype=bool) bool_index[5:30:2] = False - sub_index = index[bool_index] + sub_index = indices[bool_index] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i - sub_index = index[list(bool_index)] + sub_index = indices[list(bool_index)] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i @@ -649,32 +649,32 @@ def test_fancy(self): for i in sl: assert i == sl[sl.get_loc(i)] - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("dtype", [np.int_, np.bool_]) - def test_empty_fancy(self, index, dtype): + def test_empty_fancy(self, indices, dtype): empty_arr = np.array([], dtype=dtype) - empty_index = type(index)([]) + empty_index = type(indices)([]) - assert index[[]].identical(empty_index) - assert index[empty_arr].identical(empty_index) + assert indices[[]].identical(empty_index) + assert indices[empty_arr].identical(empty_index) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_empty_fancy_raises(self, index): + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_empty_fancy_raises(self, indices): # pd.DatetimeIndex is excluded, because it overrides getitem and should # be tested separately. empty_farr = np.array([], dtype=np.float_) - empty_index = type(index)([]) + empty_index = type(indices)([]) - assert index[[]].identical(empty_index) + assert indices[[]].identical(empty_index) # np.ndarray only accepts ndarray of int & bool dtypes, so should Index msg = r"arrays used as indices must be of integer \(or boolean\) type" with pytest.raises(IndexError, match=msg): - index[empty_farr] + indices[empty_farr] - @pytest.mark.parametrize("index", ["string"], indirect=True) - def test_intersection(self, index, sort): - first = index[:20] - second = index[:10] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_intersection(self, indices, sort): + first = indices[:20] + second = indices[:10] intersect = first.intersection(second, sort=sort) if sort is None: tm.assert_index_equal(intersect, second.sort_values()) @@ -703,16 +703,16 @@ 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("indices", ["string"], indirect=True) @pytest.mark.parametrize( "first_name,second_name,expected_name", [("A", "A", "A"), ("A", "B", None), (None, "B", None)], ) def test_intersection_name_preservation2( - self, index, first_name, second_name, expected_name, sort + self, indices, first_name, second_name, expected_name, sort ): - first = index[5:20] - second = index[:10] + first = indices[5:20] + second = indices[:10] first.name = first_name second.name = second_name intersect = first.intersection(second, sort=sort) @@ -782,11 +782,11 @@ 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] - everything = index[:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union(self, indices, sort): + first = indices[5:20] + second = indices[:10] + everything = indices[:20] union = first.union(second, sort=sort) if sort is None: @@ -820,12 +820,12 @@ 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): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_from_iterables(self, indices, klass, sort): # GH 10149 - first = index[5:20] - second = index[:10] - everything = index[:20] + first = indices[5:20] + second = indices[:10] + everything = indices[:20] case = klass(second.values) result = first.union(case, sort=sort) @@ -833,9 +833,9 @@ 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] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_identity(self, indices, sort): + first = indices[5:20] union = first.union(first, sort=sort) # i.e. identity is not preserved when sort is True @@ -1004,12 +1004,12 @@ 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("indices", ["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] - second = index[:10] - answer = index[10:20] + def test_difference_name_preservation(self, indices, second_name, expected, sort): + first = indices[5:20] + second = indices[:10] + answer = indices[10:20] first.name = "name" second.name = second_name @@ -1022,31 +1022,31 @@ 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] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_empty_arg(self, indices, sort): + first = indices[5:20] 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] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_identity(self, indices, sort): + first = indices[5:20] 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] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_sort(self, indices, sort): + first = indices[5:20] + second = indices[:10] result = first.difference(second, sort) - expected = index[10:20] + expected = indices[10:20] if sort is None: expected = expected.sort_values() @@ -1161,7 +1161,7 @@ def test_intersection_difference(self, indices, sort): tm.assert_index_equal(inter, diff) @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", False), ("bool", False), @@ -1170,13 +1170,13 @@ def test_intersection_difference(self, indices, sort): ("datetime", False), ("float", True), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_numeric(self, index, expected): - assert index.is_numeric() is expected + def test_is_numeric(self, indices, expected): + assert indices.is_numeric() is expected @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", True), ("bool", True), @@ -1185,13 +1185,13 @@ def test_is_numeric(self, index, expected): ("datetime", False), ("float", False), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_object(self, index, expected): - assert index.is_object() is expected + def test_is_object(self, indices, expected): + assert indices.is_object() is expected @pytest.mark.parametrize( - "index, expected", + "indices, expected", [ ("string", False), ("bool", False), @@ -1200,10 +1200,10 @@ def test_is_object(self, index, expected): ("datetime", True), ("float", False), ], - indirect=["index"], + indirect=["indices"], ) - def test_is_all_dates(self, index, expected): - assert index.is_all_dates is expected + def test_is_all_dates(self, indices, expected): + assert indices.is_all_dates is expected def test_summary(self, indices): self._check_method_works(Index._summary, indices) @@ -1583,37 +1583,37 @@ def test_slice_locs_negative_step(self, in_slice, expected): expected = pd.Index(list(expected)) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label(self, index): - n = len(index) - drop = index[list(range(5, 10))] - dropped = index.drop(drop) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label(self, indices): + n = len(indices) + drop = indices[list(range(5, 10))] + dropped = indices.drop(drop) - expected = index[list(range(5)) + list(range(10, n))] + expected = indices[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = index.drop(index[0]) - expected = index[1:] + dropped = indices.drop(indices[0]) + expected = indices[1:] tm.assert_index_equal(dropped, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) @pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]]) - def test_drop_by_str_label_raises_missing_keys(self, index, keys): + def test_drop_by_str_label_raises_missing_keys(self, indices, keys): with pytest.raises(KeyError, match=""): - index.drop(keys) + indices.drop(keys) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) - def test_drop_by_str_label_errors_ignore(self, index): - n = len(index) - drop = index[list(range(5, 10))] + @pytest.mark.parametrize("indices", ["string", "int", "float"], indirect=True) + def test_drop_by_str_label_errors_ignore(self, indices): + n = len(indices) + drop = indices[list(range(5, 10))] mixed = drop.tolist() + ["foo"] - dropped = index.drop(mixed, errors="ignore") + dropped = indices.drop(mixed, errors="ignore") - expected = index[list(range(5)) + list(range(10, n))] + expected = indices[list(range(5)) + list(range(10, n))] tm.assert_index_equal(dropped, expected) - dropped = index.drop(["foo", "bar"], errors="ignore") - expected = index[list(range(n))] + dropped = indices.drop(["foo", "bar"], errors="ignore") + expected = indices[list(range(n))] tm.assert_index_equal(dropped, expected) def test_drop_by_numeric_label_loc(self): @@ -1733,18 +1733,18 @@ def test_set_value_deprecated(self): assert arr[1] == 80 @pytest.mark.parametrize( - "index", ["string", "int", "datetime", "timedelta"], indirect=True + "indices", ["string", "int", "datetime", "timedelta"], indirect=True ) - def test_get_value(self, index): + def test_get_value(self, indices): # TODO: Remove function? GH 19728 values = np.random.randn(100) - value = index[67] + value = indices[67] with pytest.raises(AttributeError, match="has no attribute '_values'"): # Index.get_value requires a Series, not an ndarray - index.get_value(values, value) + indices.get_value(values, value) - result = index.get_value(Series(values, index=values), value) + result = indices.get_value(Series(values, index=values), value) tm.assert_almost_equal(result, values[67]) @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}]) @@ -1866,10 +1866,10 @@ def test_boolean_cmp(self, values): tm.assert_numpy_array_equal(result, expected) - @pytest.mark.parametrize("index", ["string"], indirect=True) + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")]) - def test_get_level_values(self, index, name, level): - expected = index.copy() + def test_get_level_values(self, indices, name, level): + expected = indices.copy() if name: expected.name = name @@ -1881,13 +1881,13 @@ def test_slice_keep_name(self): assert index.name == index[1:].name @pytest.mark.parametrize( - "index", + "indices", ["unicode", "string", "datetime", "int", "uint", "float"], indirect=True, ) - def test_join_self(self, index, join_type): - joined = index.join(index, how=join_type) - assert index is joined + def test_join_self(self, indices, join_type): + joined = indices.join(indices, how=join_type) + assert indices is joined @pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"]) def test_str_attribute(self, method): From 2a3e611e2d401972d8fe72593ad4b966b0002fd7 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 21:35:40 +0100 Subject: [PATCH 5/6] using string_index fixture instead of @pytest.mark.parametrize(indices, [string], indirect=True) --- pandas/tests/indexes/conftest.py | 8 +++ pandas/tests/indexes/test_base.py | 93 ++++++++++++++----------------- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/pandas/tests/indexes/conftest.py b/pandas/tests/indexes/conftest.py index a9fb228073ab4..e2f278b4fa62f 100644 --- a/pandas/tests/indexes/conftest.py +++ b/pandas/tests/indexes/conftest.py @@ -1,5 +1,7 @@ import pytest +import pandas._testing as tm + @pytest.fixture(params=[None, False]) def sort(request): @@ -16,3 +18,9 @@ def sort(request): in in the Index setops methods. """ return request.param + + +@pytest.fixture +def string_index(): + """ Simple string index fixture """ + return tm.makeStringIndex(100) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 93176474526a7..25a97505222b1 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -70,19 +70,17 @@ def test_copy_and_deepcopy(self, indices): def test_constructor_regular(self, indices): tm.assert_contains_all(indices, indices) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_constructor_casting(self, indices): + def test_constructor_casting(self, string_index): # casting - arr = np.array(indices) + arr = np.array(string_index) new_index = Index(arr) tm.assert_contains_all(arr, new_index) - tm.assert_index_equal(indices, new_index) + tm.assert_index_equal(string_index, new_index) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_constructor_copy(self, indices): + def test_constructor_copy(self, string_index): # copy # index = self.create_index() - arr = np.array(indices) + arr = np.array(string_index) new_index = Index(arr, copy=True, name="name") assert isinstance(new_index, Index) assert new_index.name == "name" @@ -629,17 +627,16 @@ 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("indices", ["string"], indirect=True) - def test_booleanindex(self, indices): - bool_index = np.ones(len(indices), dtype=bool) + def test_booleanindex(self, string_index): + bool_index = np.ones(len(string_index), dtype=bool) bool_index[5:30:2] = False - sub_index = indices[bool_index] + sub_index = string_index[bool_index] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i - sub_index = indices[list(bool_index)] + sub_index = string_index[list(bool_index)] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i @@ -671,10 +668,9 @@ def test_empty_fancy_raises(self, indices): with pytest.raises(IndexError, match=msg): indices[empty_farr] - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_intersection(self, indices, sort): - first = indices[:20] - second = indices[:10] + def test_intersection(self, string_index, sort): + first = string_index[:20] + second = string_index[:10] intersect = first.intersection(second, sort=sort) if sort is None: tm.assert_index_equal(intersect, second.sort_values()) @@ -703,16 +699,15 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort): assert result.name == expected.name tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize( "first_name,second_name,expected_name", [("A", "A", "A"), ("A", "B", None), (None, "B", None)], ) def test_intersection_name_preservation2( - self, indices, first_name, second_name, expected_name, sort + self, string_index, first_name, second_name, expected_name, sort ): - first = indices[5:20] - second = indices[:10] + first = string_index[5:20] + second = string_index[:10] first.name = first_name second.name = second_name intersect = first.intersection(second, sort=sort) @@ -782,11 +777,10 @@ 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("indices", ["string"], indirect=True) - def test_union(self, indices, sort): - first = indices[5:20] - second = indices[:10] - everything = indices[:20] + def test_union(self, string_index, sort): + first = string_index[5:20] + second = string_index[:10] + everything = string_index[:20] union = first.union(second, sort=sort) if sort is None: @@ -820,12 +814,11 @@ 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("indices", ["string"], indirect=True) - def test_union_from_iterables(self, indices, klass, sort): + def test_union_from_iterables(self, string_index, klass, sort): # GH 10149 - first = indices[5:20] - second = indices[:10] - everything = indices[:20] + first = string_index[5:20] + second = string_index[:10] + everything = string_index[:20] case = klass(second.values) result = first.union(case, sort=sort) @@ -833,9 +826,8 @@ def test_union_from_iterables(self, indices, klass, sort): tm.assert_index_equal(result, everything.sort_values()) assert tm.equalContents(result, everything) - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_union_identity(self, indices, sort): - first = indices[5:20] + def test_union_identity(self, string_index, sort): + first = string_index[5:20] union = first.union(first, sort=sort) # i.e. identity is not preserved when sort is True @@ -1004,12 +996,13 @@ def test_append_empty_preserve_name(self, name, expected): result = left.append(right) assert result.name == expected - @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")]) - def test_difference_name_preservation(self, indices, second_name, expected, sort): - first = indices[5:20] - second = indices[:10] - answer = indices[10:20] + def test_difference_name_preservation( + self, string_index, second_name, expected, sort + ): + first = string_index[5:20] + second = string_index[:10] + answer = string_index[10:20] first.name = "name" second.name = second_name @@ -1022,31 +1015,28 @@ def test_difference_name_preservation(self, indices, second_name, expected, sort else: assert result.name == expected - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_empty_arg(self, indices, sort): - first = indices[5:20] + def test_difference_empty_arg(self, string_index, sort): + first = string_index[5:20] first.name = "name" result = first.difference([], sort) assert tm.equalContents(result, first) assert result.name == first.name - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_identity(self, indices, sort): - first = indices[5:20] + def test_difference_identity(self, string_index, sort): + first = string_index[5:20] first.name = "name" result = first.difference(first, sort) assert len(result) == 0 assert result.name == first.name - @pytest.mark.parametrize("indices", ["string"], indirect=True) - def test_difference_sort(self, indices, sort): - first = indices[5:20] - second = indices[:10] + def test_difference_sort(self, string_index, sort): + first = string_index[5:20] + second = string_index[:10] result = first.difference(second, sort) - expected = indices[10:20] + expected = string_index[10:20] if sort is None: expected = expected.sort_values() @@ -1866,10 +1856,9 @@ def test_boolean_cmp(self, values): tm.assert_numpy_array_equal(result, expected) - @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")]) - def test_get_level_values(self, indices, name, level): - expected = indices.copy() + def test_get_level_values(self, string_index, name, level): + expected = string_index.copy() if name: expected.name = name From 85328aae9cf83fe0241f77152a994fa2b872f77c Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Sun, 29 Mar 2020 18:07:44 +0200 Subject: [PATCH 6/6] Revert "using string_index fixture instead of @pytest.mark.parametrize(indices, [string], indirect=True)" This reverts commit 2a3e611e2d401972d8fe72593ad4b966b0002fd7. --- pandas/tests/indexes/conftest.py | 8 --- pandas/tests/indexes/test_base.py | 93 +++++++++++++++++-------------- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/pandas/tests/indexes/conftest.py b/pandas/tests/indexes/conftest.py index e2f278b4fa62f..a9fb228073ab4 100644 --- a/pandas/tests/indexes/conftest.py +++ b/pandas/tests/indexes/conftest.py @@ -1,7 +1,5 @@ import pytest -import pandas._testing as tm - @pytest.fixture(params=[None, False]) def sort(request): @@ -18,9 +16,3 @@ def sort(request): in in the Index setops methods. """ return request.param - - -@pytest.fixture -def string_index(): - """ Simple string index fixture """ - return tm.makeStringIndex(100) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 5041057449949..35ee81229b716 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -70,17 +70,19 @@ def test_copy_and_deepcopy(self, indices): def test_constructor_regular(self, indices): tm.assert_contains_all(indices, indices) - def test_constructor_casting(self, string_index): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_casting(self, indices): # casting - arr = np.array(string_index) + arr = np.array(indices) new_index = Index(arr) tm.assert_contains_all(arr, new_index) - tm.assert_index_equal(string_index, new_index) + tm.assert_index_equal(indices, new_index) - def test_constructor_copy(self, string_index): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_constructor_copy(self, indices): # copy # index = self.create_index() - arr = np.array(string_index) + arr = np.array(indices) new_index = Index(arr, copy=True, name="name") assert isinstance(new_index, Index) assert new_index.name == "name" @@ -627,16 +629,17 @@ 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)] - def test_booleanindex(self, string_index): - bool_index = np.ones(len(string_index), dtype=bool) + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_booleanindex(self, indices): + bool_index = np.ones(len(indices), dtype=bool) bool_index[5:30:2] = False - sub_index = string_index[bool_index] + sub_index = indices[bool_index] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i - sub_index = string_index[list(bool_index)] + sub_index = indices[list(bool_index)] for i, val in enumerate(sub_index): assert sub_index.get_loc(val) == i @@ -668,9 +671,10 @@ def test_empty_fancy_raises(self, indices): with pytest.raises(IndexError, match=msg): indices[empty_farr] - def test_intersection(self, string_index, sort): - first = string_index[:20] - second = string_index[:10] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_intersection(self, indices, sort): + first = indices[:20] + second = indices[:10] intersect = first.intersection(second, sort=sort) if sort is None: tm.assert_index_equal(intersect, second.sort_values()) @@ -699,15 +703,16 @@ def test_intersection_name_preservation(self, index2, keeps_name, sort): assert result.name == expected.name tm.assert_index_equal(result, expected) + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize( "first_name,second_name,expected_name", [("A", "A", "A"), ("A", "B", None), (None, "B", None)], ) def test_intersection_name_preservation2( - self, string_index, first_name, second_name, expected_name, sort + self, indices, first_name, second_name, expected_name, sort ): - first = string_index[5:20] - second = string_index[:10] + first = indices[5:20] + second = indices[:10] first.name = first_name second.name = second_name intersect = first.intersection(second, sort=sort) @@ -777,10 +782,11 @@ def test_chained_union(self, sort): expected = j1.union(j2, sort=sort).union(j3, sort=sort) tm.assert_index_equal(union, expected) - def test_union(self, string_index, sort): - first = string_index[5:20] - second = string_index[:10] - everything = string_index[:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union(self, indices, sort): + first = indices[5:20] + second = indices[:10] + everything = indices[:20] union = first.union(second, sort=sort) if sort is None: @@ -814,11 +820,12 @@ def test_union_sort_special_true(self, slice_): tm.assert_index_equal(result, expected) @pytest.mark.parametrize("klass", [np.array, Series, list]) - def test_union_from_iterables(self, string_index, klass, sort): + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_from_iterables(self, indices, klass, sort): # GH 10149 - first = string_index[5:20] - second = string_index[:10] - everything = string_index[:20] + first = indices[5:20] + second = indices[:10] + everything = indices[:20] case = klass(second.values) result = first.union(case, sort=sort) @@ -826,8 +833,9 @@ def test_union_from_iterables(self, string_index, klass, sort): tm.assert_index_equal(result, everything.sort_values()) assert tm.equalContents(result, everything) - def test_union_identity(self, string_index, sort): - first = string_index[5:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_union_identity(self, indices, sort): + first = indices[5:20] union = first.union(first, sort=sort) # i.e. identity is not preserved when sort is True @@ -996,13 +1004,12 @@ def test_append_empty_preserve_name(self, name, expected): result = left.append(right) assert result.name == expected + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")]) - def test_difference_name_preservation( - self, string_index, second_name, expected, sort - ): - first = string_index[5:20] - second = string_index[:10] - answer = string_index[10:20] + def test_difference_name_preservation(self, indices, second_name, expected, sort): + first = indices[5:20] + second = indices[:10] + answer = indices[10:20] first.name = "name" second.name = second_name @@ -1015,28 +1022,31 @@ def test_difference_name_preservation( else: assert result.name == expected - def test_difference_empty_arg(self, string_index, sort): - first = string_index[5:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_empty_arg(self, indices, sort): + first = indices[5:20] first.name = "name" result = first.difference([], sort) assert tm.equalContents(result, first) assert result.name == first.name - def test_difference_identity(self, string_index, sort): - first = string_index[5:20] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_identity(self, indices, sort): + first = indices[5:20] first.name = "name" result = first.difference(first, sort) assert len(result) == 0 assert result.name == first.name - def test_difference_sort(self, string_index, sort): - first = string_index[5:20] - second = string_index[:10] + @pytest.mark.parametrize("indices", ["string"], indirect=True) + def test_difference_sort(self, indices, sort): + first = indices[5:20] + second = indices[:10] result = first.difference(second, sort) - expected = string_index[10:20] + expected = indices[10:20] if sort is None: expected = expected.sort_values() @@ -1856,9 +1866,10 @@ def test_boolean_cmp(self, values): tm.assert_numpy_array_equal(result, expected) + @pytest.mark.parametrize("indices", ["string"], indirect=True) @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")]) - def test_get_level_values(self, string_index, name, level): - expected = string_index.copy() + def test_get_level_values(self, indices, name, level): + expected = indices.copy() if name: expected.name = name