Skip to content

Commit

Permalink
Use less idx fixture in multi
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Dec 24, 2023
1 parent 9f9a643 commit e4f81bb
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 83 deletions.
17 changes: 11 additions & 6 deletions pandas/tests/indexes/multi/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexes/multi/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/multi/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/multi/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/multi/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexes/multi/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/indexes/multi/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
)
Expand Down
21 changes: 12 additions & 9 deletions pandas/tests/indexes/multi/test_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)))],
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)


Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/multi/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/indexes/multi/test_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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])
Expand All @@ -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
3 changes: 2 additions & 1 deletion pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/indexes/multi/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/indexes/multi/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit e4f81bb

Please sign in to comment.