From e4f81bb3aff832bc1f23a109624b792e5f08f6e2 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sat, 23 Dec 2023 17:42:25 -0800 Subject: [PATCH] Use less idx fixture in multi --- pandas/tests/indexes/multi/test_analytics.py | 17 +++++---- pandas/tests/indexes/multi/test_astype.py | 7 ++-- pandas/tests/indexes/multi/test_compat.py | 6 ++-- .../tests/indexes/multi/test_constructors.py | 3 +- pandas/tests/indexes/multi/test_conversion.py | 3 +- pandas/tests/indexes/multi/test_copy.py | 9 +++-- pandas/tests/indexes/multi/test_duplicates.py | 10 ++++-- .../tests/indexes/multi/test_equivalence.py | 21 ++++++----- pandas/tests/indexes/multi/test_formats.py | 6 ++-- pandas/tests/indexes/multi/test_get_set.py | 3 +- pandas/tests/indexes/multi/test_integrity.py | 17 +++++---- pandas/tests/indexes/multi/test_join.py | 3 +- pandas/tests/indexes/multi/test_missing.py | 10 +++--- pandas/tests/indexes/multi/test_reindex.py | 10 ++++-- pandas/tests/indexes/multi/test_reshape.py | 18 +++++++--- pandas/tests/indexes/multi/test_setops.py | 36 +++++++++++++------ pandas/tests/indexes/multi/test_sorting.py | 31 ++++------------ pandas/tests/indexes/multi/test_take.py | 6 +++- 18 files changed, 133 insertions(+), 83 deletions(-) diff --git a/pandas/tests/indexes/multi/test_analytics.py b/pandas/tests/indexes/multi/test_analytics.py index 87f1439db5fc8..a0e7510fbe2dc 100644 --- a/pandas/tests/indexes/multi/test_analytics.py +++ b/pandas/tests/indexes/multi/test_analytics.py @@ -11,13 +11,15 @@ import pandas._testing as tm -def test_infer_objects(idx): +def test_infer_objects(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) with pytest.raises(NotImplementedError, match="to_frame"): idx.infer_objects() -def test_shift(idx): +def test_shift(): # GH8083 test the base class for shift + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = ( "This method is only implemented for DatetimeIndex, PeriodIndex and " "TimedeltaIndex; Got type MultiIndex" @@ -76,8 +78,9 @@ def test_truncate_multiindex(): # TODO: reshape -def test_reorder_levels(idx): +def test_reorder_levels(): # this blows up + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) with pytest.raises(IndexError, match="^Too many levels"): idx.reorder_levels([2, 1, 0]) @@ -174,9 +177,9 @@ def test_sub(idx): first.tolist() - idx[-3:] -def test_map(idx): +def test_map(): # callable - index = idx + index = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = index.map(lambda x: x) tm.assert_index_equal(result, index) @@ -235,10 +238,11 @@ def test_map_dictlike(idx, mapper): ], ids=lambda func: func.__name__, ) -def test_numpy_ufuncs(idx, func): +def test_numpy_ufuncs(func): # test ufuncs of numpy. see: # https://numpy.org/doc/stable/reference/ufuncs.html + idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]]) expected_exception = TypeError msg = ( "loop of ufunc does not support argument 0 of type tuple which " @@ -254,6 +258,7 @@ def test_numpy_ufuncs(idx, func): ids=lambda func: func.__name__, ) def test_numpy_type_funcs(idx, func): + idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]]) msg = ( f"ufunc '{func.__name__}' not supported for the input types, and the inputs " "could not be safely coerced to any supported types according to " diff --git a/pandas/tests/indexes/multi/test_astype.py b/pandas/tests/indexes/multi/test_astype.py index 29908537fbe59..1f9f8d91ad970 100644 --- a/pandas/tests/indexes/multi/test_astype.py +++ b/pandas/tests/indexes/multi/test_astype.py @@ -3,10 +3,12 @@ from pandas.core.dtypes.dtypes import CategoricalDtype +from pandas import MultiIndex import pandas._testing as tm -def test_astype(idx): +def test_astype(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]], names=["foo"]) expected = idx.copy() actual = idx.astype("O") tm.assert_copy(actual.levels, expected.levels) @@ -18,7 +20,8 @@ def test_astype(idx): @pytest.mark.parametrize("ordered", [True, False]) -def test_astype_category(idx, ordered): +def test_astype_category(ordered): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) # GH 18630 msg = "> 1 ndim Categorical are not supported at this time" with pytest.raises(NotImplementedError, match=msg): diff --git a/pandas/tests/indexes/multi/test_compat.py b/pandas/tests/indexes/multi/test_compat.py index 27a8c6e9b7158..4bc73f8272d08 100644 --- a/pandas/tests/indexes/multi/test_compat.py +++ b/pandas/tests/indexes/multi/test_compat.py @@ -6,7 +6,8 @@ import pandas._testing as tm -def test_numeric_compat(idx): +def test_numeric_compat(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) with pytest.raises(TypeError, match="cannot perform __mul__"): idx * 1 @@ -29,7 +30,8 @@ def test_numeric_compat(idx): @pytest.mark.parametrize("method", ["all", "any", "__invert__"]) -def test_logical_compat(idx, method): +def test_logical_compat(method): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = f"cannot perform {method}" with pytest.raises(TypeError, match=msg): diff --git a/pandas/tests/indexes/multi/test_constructors.py b/pandas/tests/indexes/multi/test_constructors.py index 8456e6a7acba5..0ad8831805087 100644 --- a/pandas/tests/indexes/multi/test_constructors.py +++ b/pandas/tests/indexes/multi/test_constructors.py @@ -385,7 +385,8 @@ def test_from_tuples_empty(): tm.assert_index_equal(result, expected) -def test_from_tuples_index_values(idx): +def test_from_tuples_index_values(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = MultiIndex.from_tuples(idx) assert (result.values == idx.values).all() diff --git a/pandas/tests/indexes/multi/test_conversion.py b/pandas/tests/indexes/multi/test_conversion.py index 3c2ca045d6f99..0109d67cb7dde 100644 --- a/pandas/tests/indexes/multi/test_conversion.py +++ b/pandas/tests/indexes/multi/test_conversion.py @@ -9,7 +9,8 @@ import pandas._testing as tm -def test_to_numpy(idx): +def test_to_numpy(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = idx.to_numpy() exp = idx.values tm.assert_numpy_array_equal(result, exp) diff --git a/pandas/tests/indexes/multi/test_copy.py b/pandas/tests/indexes/multi/test_copy.py index 2e09a580f9528..504496ea527cc 100644 --- a/pandas/tests/indexes/multi/test_copy.py +++ b/pandas/tests/indexes/multi/test_copy.py @@ -26,19 +26,22 @@ def assert_multiindex_copied(copy, original): assert copy.sortorder == original.sortorder -def test_copy(idx): +def test_copy(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) i_copy = idx.copy() assert_multiindex_copied(i_copy, idx) -def test_shallow_copy(idx): +def test_shallow_copy(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) i_copy = idx._view() assert_multiindex_copied(i_copy, idx) -def test_view(idx): +def test_view(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) i_view = idx.view() assert_multiindex_copied(i_view, idx) diff --git a/pandas/tests/indexes/multi/test_duplicates.py b/pandas/tests/indexes/multi/test_duplicates.py index 6c6d9022b1af3..faf83407cfa50 100644 --- a/pandas/tests/indexes/multi/test_duplicates.py +++ b/pandas/tests/indexes/multi/test_duplicates.py @@ -145,13 +145,19 @@ def test_duplicate_meta_data(): assert idx.drop_duplicates().names == idx.names -def test_has_duplicates(idx, idx_dup): - # see fixtures +def test_has_duplicates(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) assert idx.is_unique is True assert idx.has_duplicates is False + + +def test_has_duplicates_with_dups(idx_dup): + # see fixtures assert idx_dup.is_unique is False assert idx_dup.has_duplicates is True + +def test_has_duplicates_other(): mi = MultiIndex( levels=[[0, 1], [0, 1, 2]], codes=[[0, 0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 0, 1, 2]] ) diff --git a/pandas/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index 9babbd5b8d56d..079f6194c2ff9 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -12,7 +12,8 @@ import pandas._testing as tm -def test_equals(idx): +def test_equals(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) assert idx.equals(idx) assert idx.equals(idx.copy()) assert idx.equals(idx.astype(object)) @@ -26,10 +27,6 @@ def test_equals(idx): assert idx.equals(same_values) assert same_values.equals(idx) - if idx.nlevels == 1: - # do not test MultiIndex - assert not idx.equals(Series(idx)) - def test_equals_op(idx): # GH9947, GH10637 @@ -132,7 +129,8 @@ def test_compare_tuple_strs(): tm.assert_numpy_array_equal(result, expected) -def test_equals_multi(idx): +def test_equals_multi(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) assert idx.equals(idx) assert not idx.equals(idx.values) assert idx.equals(Index(idx.values)) @@ -141,6 +139,8 @@ def test_equals_multi(idx): assert not idx.equals(idx[:-1]) assert not idx.equals(idx[-1]) + +def test_equals_multi_different_levels(idx): # different number of levels index = MultiIndex( levels=[Index(list(range(4))), Index(list(range(4))), Index(list(range(4)))], @@ -181,7 +181,8 @@ def test_equals_multi(idx): assert not idx.equals(index) -def test_identical(idx): +def test_identical(): + idx = MultiIndex(levels=[[0, 1], [2, 3]], codes=[[0, 1], [0, 1]]) mi = idx.copy() mi2 = idx.copy() assert mi.identical(mi2) @@ -249,12 +250,14 @@ def test_is_(): assert not mi5.is_(mi) -def test_is_all_dates(idx): +def test_is_all_dates(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) assert not idx._is_all_dates -def test_is_numeric(idx): +def test_is_numeric(): # MultiIndex is never numeric + idx = MultiIndex(levels=[["A", "B"]], codes=[[0, 1]]) assert not is_any_real_numeric_dtype(idx) diff --git a/pandas/tests/indexes/multi/test_formats.py b/pandas/tests/indexes/multi/test_formats.py index 52ff3109128f2..b3988e824775b 100644 --- a/pandas/tests/indexes/multi/test_formats.py +++ b/pandas/tests/indexes/multi/test_formats.py @@ -9,8 +9,9 @@ import pandas._testing as tm -def test_format(idx): +def test_format(): msg = "MultiIndex.format is deprecated" + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) with tm.assert_produces_warning(FutureWarning, match=msg): idx.format() idx[:0].format() @@ -70,8 +71,9 @@ def test_unicode_string_with_unicode(): str(idx) -def test_repr_max_seq_item_setting(idx): +def test_repr_max_seq_item_setting(): # GH10182 + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) idx = idx.repeat(50) with pd.option_context("display.max_seq_items", None): repr(idx) diff --git a/pandas/tests/indexes/multi/test_get_set.py b/pandas/tests/indexes/multi/test_get_set.py index 6eeaeb6711d03..969cbf6fb8cd9 100644 --- a/pandas/tests/indexes/multi/test_get_set.py +++ b/pandas/tests/indexes/multi/test_get_set.py @@ -242,7 +242,8 @@ def test_set_codes(idx): assert result.equals(expected) -def test_set_levels_codes_names_bad_input(idx): +def test_set_levels_codes_names_bad_input(): + idx = MultiIndex(levels=[["A", "B"], ["B", "C"]], codes=[[0, 1], [0, 1]]) levels, codes = idx.levels, idx.codes names = idx.names diff --git a/pandas/tests/indexes/multi/test_integrity.py b/pandas/tests/indexes/multi/test_integrity.py index d956747cbc859..93262da50b0f6 100644 --- a/pandas/tests/indexes/multi/test_integrity.py +++ b/pandas/tests/indexes/multi/test_integrity.py @@ -163,10 +163,11 @@ def test_take_invalid_kwargs(): idx.take(indices, mode="clip") -def test_isna_behavior(idx): +def test_isna_behavior(): # should not segfault GH5123 # NOTE: if MI representation changes, may make sense to allow # isna(MI) + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = "isna is not defined for MultiIndex" with pytest.raises(NotImplementedError, match=msg): pd.isna(idx) @@ -208,12 +209,14 @@ def test_mi_hashtable_populated_attribute_error(monkeypatch): df["a"].foo() -def test_can_hold_identifiers(idx): +def test_can_hold_identifiers(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) key = idx[0] assert idx._can_hold_identifiers_and_holds_name(key) is True -def test_metadata_immutable(idx): +def test_metadata_immutable(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) levels, codes = idx.levels, idx.codes # shouldn't be able to set at either the top level or base level mutable_regex = re.compile("does not support mutable operations") @@ -265,7 +268,8 @@ def test_rangeindex_fallback_coercion_bug(): tm.assert_index_equal(result, expected) -def test_memory_usage(idx): +def test_memory_usage(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = idx.memory_usage() if len(idx): idx.get_loc(idx[0]) @@ -285,5 +289,6 @@ def test_memory_usage(idx): assert result == 0 -def test_nlevels(idx): - assert idx.nlevels == 2 +def test_nlevels(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) + assert idx.nlevels == 1 diff --git a/pandas/tests/indexes/multi/test_join.py b/pandas/tests/indexes/multi/test_join.py index edd0feaaa1159..1b88ef790723c 100644 --- a/pandas/tests/indexes/multi/test_join.py +++ b/pandas/tests/indexes/multi/test_join.py @@ -50,7 +50,8 @@ def test_join_level_corner_case(idx): idx.join(idx, level=1) -def test_join_self(idx, join_type): +def test_join_self(join_type): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = idx.join(idx, how=join_type) expected = idx if join_type == "outer": diff --git a/pandas/tests/indexes/multi/test_missing.py b/pandas/tests/indexes/multi/test_missing.py index 14ffc42fb4b59..5e3c545f6a35f 100644 --- a/pandas/tests/indexes/multi/test_missing.py +++ b/pandas/tests/indexes/multi/test_missing.py @@ -6,8 +6,9 @@ import pandas._testing as tm -def test_fillna(idx): +def test_fillna(): # GH 11343 + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = "isna is not defined for MultiIndex" with pytest.raises(NotImplementedError, match=msg): idx.fillna(idx[0]) @@ -53,18 +54,19 @@ def test_dropna(): tm.assert_index_equal(idx.dropna(how="all"), expected) -def test_nulls(idx): +def test_nulls(): # this is really a smoke test for the methods # as these are adequately tested for function elsewhere - + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = "isna is not defined for MultiIndex" with pytest.raises(NotImplementedError, match=msg): idx.isna() @pytest.mark.xfail(reason="isna is not defined for MultiIndex") -def test_hasnans_isnans(idx): +def test_hasnans_isnans(): # GH 11343, added tests for hasnans / isnans + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) index = idx.copy() # cases in indices doesn't include NaN diff --git a/pandas/tests/indexes/multi/test_reindex.py b/pandas/tests/indexes/multi/test_reindex.py index d1b4fe8b98760..007480db1de1b 100644 --- a/pandas/tests/indexes/multi/test_reindex.py +++ b/pandas/tests/indexes/multi/test_reindex.py @@ -9,7 +9,12 @@ import pandas._testing as tm -def test_reindex(idx): +def test_reindex(): + idx = MultiIndex( + levels=[list(range(5)), list(range(1, 6))], + codes=[list(range(5)), list(range(5))], + names=["first", "second"], + ) result, indexer = idx.reindex(list(idx[:4])) assert isinstance(result, MultiIndex) assert result.names == ["first", "second"] @@ -92,7 +97,8 @@ def test_reindex_lvl_preserves_type_if_target_is_empty_list_or_array( assert mi.reindex([], level=1)[0].levels[1].dtype == dti.dtype -def test_reindex_base(idx): +def test_reindex_base(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) expected = np.arange(idx.size, dtype=np.intp) actual = idx.get_indexer(idx) diff --git a/pandas/tests/indexes/multi/test_reshape.py b/pandas/tests/indexes/multi/test_reshape.py index 06dbb33aadf97..13d5bd1ae8d65 100644 --- a/pandas/tests/indexes/multi/test_reshape.py +++ b/pandas/tests/indexes/multi/test_reshape.py @@ -12,7 +12,12 @@ import pandas._testing as tm -def test_insert(idx): +def test_insert(): + idx = MultiIndex( + levels=[["bar", "foo"], ["two", "one"]], + codes=[[0, 1], [0, 1]], + names=["first", "second"], + ) # key contained in all levels new_index = idx.insert(0, ("bar", "two")) assert new_index.equal_levels(idx) @@ -34,6 +39,8 @@ def test_insert(idx): with pytest.raises(ValueError, match=msg): idx.insert(0, ("foo2",)) + +def test_insert_reindex(): left = pd.DataFrame([["a", "b", 0], ["b", "d", 1]], columns=["1st", "2nd", "3rd"]) left.set_index(["1st", "2nd"], inplace=True) ts = left["3rd"].copy(deep=True) @@ -90,7 +97,8 @@ def test_insert2(): tm.assert_series_equal(left, right) -def test_append(idx): +def test_append(): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) result = idx[:3].append(idx[3:]) assert result.equals(idx) @@ -201,14 +209,16 @@ def test_repeat(): tm.assert_index_equal(m.repeat(reps), expected) -def test_insert_base(idx): +def test_insert_base(): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) result = idx[1:4] # test 0th element assert idx[0:4].equals(result.insert(0, idx[0])) -def test_delete_base(idx): +def test_delete_base(): + idx = MultiIndex(levels=[list(range(6))], codes=[list(range(6))]) expected = idx[1:] result = idx.delete(0) assert result.equals(expected) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index 0abb56ecf9de7..025381616743a 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -21,15 +21,17 @@ @pytest.mark.parametrize( "method", ["intersection", "union", "difference", "symmetric_difference"] ) -def test_set_ops_error_cases(idx, case, sort, method): +def test_set_ops_error_cases(case, sort, method): # non-iterable input + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = "Input must be Index or array-like" with pytest.raises(TypeError, match=msg): getattr(idx, method)(case, sort=sort) @pytest.mark.parametrize("klass", [MultiIndex, np.array, Series, list]) -def test_intersection_base(idx, sort, klass): +def test_intersection_base(sort, klass): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) first = idx[2::-1] # first 3 elements reversed second = idx[:5] @@ -50,7 +52,8 @@ def test_intersection_base(idx, sort, klass): @pytest.mark.arm_slow @pytest.mark.parametrize("klass", [MultiIndex, np.array, Series, list]) -def test_union_base(idx, sort, klass): +def test_union_base(sort, klass): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) first = idx[::-1] second = idx[:5] @@ -69,7 +72,8 @@ def test_union_base(idx, sort, klass): first.union([1, 2, 3], sort=sort) -def test_difference_base(idx, sort): +def test_difference_base(sort): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) second = idx[4:] answer = idx[:4] result = idx.difference(second, sort=sort) @@ -91,7 +95,8 @@ def test_difference_base(idx, sort): idx.difference([1, 2, 3], sort=sort) -def test_symmetric_difference(idx, sort): +def test_symmetric_difference(sort): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) first = idx[1:] second = idx[:-1] answer = idx[[-1, 0]] @@ -124,13 +129,18 @@ def test_multiindex_symmetric_difference(): assert result.names == [None, None] -def test_empty(idx): +def test_empty(): # GH 15270 + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) assert not idx.empty assert idx[:0].empty -def test_difference(idx, sort): +def test_difference(sort): + idx = MultiIndex( + levels=[list(range(5)), list(range(1, 6))], + codes=[list(range(5)), list(range(5))], + ) first = idx result = first.difference(idx[-3:], sort=sort) vals = idx[:-3].values @@ -237,7 +247,8 @@ def test_difference_sort_incomparable_true(): idx.difference(other, sort=True) -def test_union(idx, sort): +def test_union(sort): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) piece1 = idx[:5][::-1] piece2 = idx[3:] @@ -282,7 +293,8 @@ def test_union_with_regular_index(idx, using_infer_string): assert not result.equals(result2) -def test_intersection(idx, sort): +def test_intersection(sort): + idx = MultiIndex(levels=[list(range(5))], codes=[list(range(5))]) piece1 = idx[:5][::-1] piece2 = idx[3:] @@ -310,7 +322,8 @@ def test_intersection(idx, sort): @pytest.mark.parametrize( "method", ["intersection", "union", "difference", "symmetric_difference"] ) -def test_setop_with_categorical(idx, sort, method): +def test_setop_with_categorical(sort, method): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) other = idx.to_flat_index().astype("category") res_names = [None] * idx.nlevels @@ -323,7 +336,8 @@ def test_setop_with_categorical(idx, sort, method): tm.assert_index_equal(result, expected) -def test_intersection_non_object(idx, sort): +def test_intersection_non_object(sort): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) other = Index(range(3), name="foo") result = idx.intersection(other, sort=sort) diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index b4dcef71dcf50..4748016d2943e 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -7,11 +7,9 @@ ) from pandas import ( - CategoricalIndex, DataFrame, Index, MultiIndex, - RangeIndex, Series, Timestamp, ) @@ -19,7 +17,8 @@ from pandas.core.indexes.frozen import FrozenList -def test_sortlevel(idx): +def test_sortlevel(): + idx = MultiIndex(levels=[[0, 1], [1, 2]], codes=[[0, 1], [0, 1]]) tuples = list(idx) np.random.default_rng(2).shuffle(tuples) @@ -83,31 +82,12 @@ def test_sortlevel_na_position(): tm.assert_index_equal(result, expected) -def test_numpy_argsort(idx): +def test_numpy_argsort(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = np.argsort(idx) expected = idx.argsort() tm.assert_numpy_array_equal(result, expected) - # these are the only two types that perform - # pandas compatibility input validation - the - # rest already perform separate (or no) such - # validation via their 'values' attribute as - # defined in pandas.core.indexes/base.py - they - # cannot be changed at the moment due to - # backwards compatibility concerns - if isinstance(type(idx), (CategoricalIndex, RangeIndex)): - msg = "the 'axis' parameter is not supported" - with pytest.raises(ValueError, match=msg): - np.argsort(idx, axis=1) - - msg = "the 'kind' parameter is not supported" - with pytest.raises(ValueError, match=msg): - np.argsort(idx, kind="mergesort") - - msg = "the 'order' parameter is not supported" - with pytest.raises(ValueError, match=msg): - np.argsort(idx, order=("a", "b")) - def test_unsortedindex(): # GH 11897 @@ -277,7 +257,8 @@ def test_remove_unused_nan(level0, level1): assert "unused" not in result.levels[level] -def test_argsort(idx): +def test_argsort(): + idx = MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) result = idx.argsort() expected = idx.values.argsort() tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/indexes/multi/test_take.py b/pandas/tests/indexes/multi/test_take.py index 543cba25c373b..6ae927137a304 100644 --- a/pandas/tests/indexes/multi/test_take.py +++ b/pandas/tests/indexes/multi/test_take.py @@ -11,13 +11,17 @@ def test_take(idx): expected = idx[indexer] assert result.equals(expected) + +def test_freq(): # GH 10791 + idx = pd.MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) msg = "'MultiIndex' object has no attribute 'freq'" with pytest.raises(AttributeError, match=msg): idx.freq -def test_take_invalid_kwargs(idx): +def test_take_invalid_kwargs(): + idx = pd.MultiIndex(levels=[[0, 1]], codes=[[0, 1]]) indices = [1, 2] msg = r"take\(\) got an unexpected keyword argument 'foo'"