Skip to content

REF: organize MultiIndex indexing tests #31715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def test_create_index_existing_name(self):
def test_numeric_compat(self):

idx = self.create_index()
# Check that this doesn't cover MultiIndex case, if/when it does,
# we can remove multi.test_compat.test_numeric_compat
assert not isinstance(idx, MultiIndex)

with pytest.raises(TypeError, match="cannot perform __mul__"):
idx * 1
with pytest.raises(TypeError, match="cannot perform __rmul__"):
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/multi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def index_names():
return ["first", "second"]


@pytest.fixture
def holder():
# the MultiIndex constructor used to base compatibility with pickle
return MultiIndex


@pytest.fixture
def compat_props():
# a MultiIndex must have these properties associated with it
Expand Down
77 changes: 0 additions & 77 deletions pandas/tests/indexes/multi/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,83 +146,6 @@ def test_append_mixed_dtypes():
tm.assert_index_equal(res, exp)


def test_take(idx):
indexer = [4, 3, 0, 2]
result = idx.take(indexer)
expected = idx[indexer]
assert result.equals(expected)

# TODO: Remove Commented Code
# if not isinstance(idx,
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# GH 10791
msg = "'MultiIndex' object has no attribute 'freq'"
with pytest.raises(AttributeError, match=msg):
idx.freq


def test_take_invalid_kwargs(idx):
idx = idx
indices = [1, 2]

msg = r"take\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
idx.take(indices, foo=2)

msg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=msg):
idx.take(indices, out=indices)

msg = "the 'mode' parameter is not supported"
with pytest.raises(ValueError, match=msg):
idx.take(indices, mode="clip")


def test_take_fill_value():
# GH 12631
vals = [["A", "B"], [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]]
idx = pd.MultiIndex.from_product(vals, names=["str", "dt"])

result = idx.take(np.array([1, 0, -1]))
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
("B", pd.Timestamp("2011-01-02")),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

# fill_value
result = idx.take(np.array([1, 0, -1]), fill_value=True)
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
(np.nan, pd.NaT),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

# allow_fill=False
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
("B", pd.Timestamp("2011-01-02")),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

msg = "When allow_fill=True and fill_value is not None, all indices must be >= -1"
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -2]), fill_value=True)
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -5]), fill_value=True)

msg = "index -5 is out of bounds for( axis 0 with)? size 4"
with pytest.raises(IndexError, match=msg):
idx.take(np.array([1, -5]))


def test_iter(idx):
result = list(idx)
expected = [
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def test_ndarray_compat_properties(idx, compat_props):
idx.values.nbytes


def test_pickle_compat_construction(holder):
def test_pickle_compat_construction():
# this is testing for pickle compat
# need an object to create with
with pytest.raises(TypeError, match="Must pass both levels and codes"):
holder()
MultiIndex()
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_sorting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import random

import numpy as np
import pytest

Expand All @@ -9,8 +11,6 @@


def test_sortlevel(idx):
import random

tuples = list(idx)
random.shuffle(tuples)

Expand Down
82 changes: 82 additions & 0 deletions pandas/tests/indexes/multi/test_take.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import numpy as np
import pytest

import pandas as pd
import pandas._testing as tm


def test_take(idx):
indexer = [4, 3, 0, 2]
result = idx.take(indexer)
expected = idx[indexer]
assert result.equals(expected)

# FIXME: Remove Commented Code
# if not isinstance(idx,
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# GH 10791
msg = "'MultiIndex' object has no attribute 'freq'"
with pytest.raises(AttributeError, match=msg):
idx.freq


def test_take_invalid_kwargs(idx):
idx = idx
indices = [1, 2]

msg = r"take\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
idx.take(indices, foo=2)

msg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=msg):
idx.take(indices, out=indices)

msg = "the 'mode' parameter is not supported"
with pytest.raises(ValueError, match=msg):
idx.take(indices, mode="clip")


def test_take_fill_value():
# GH 12631
vals = [["A", "B"], [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]]
idx = pd.MultiIndex.from_product(vals, names=["str", "dt"])

result = idx.take(np.array([1, 0, -1]))
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
("B", pd.Timestamp("2011-01-02")),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

# fill_value
result = idx.take(np.array([1, 0, -1]), fill_value=True)
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
(np.nan, pd.NaT),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

# allow_fill=False
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
exp_vals = [
("A", pd.Timestamp("2011-01-02")),
("A", pd.Timestamp("2011-01-01")),
("B", pd.Timestamp("2011-01-02")),
]
expected = pd.MultiIndex.from_tuples(exp_vals, names=["str", "dt"])
tm.assert_index_equal(result, expected)

msg = "When allow_fill=True and fill_value is not None, all indices must be >= -1"
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -2]), fill_value=True)
with pytest.raises(ValueError, match=msg):
idx.take(np.array([1, 0, -5]), fill_value=True)

msg = "index -5 is out of bounds for( axis 0 with)? size 4"
with pytest.raises(IndexError, match=msg):
idx.take(np.array([1, -5]))