Skip to content

Commit e77e428

Browse files
elmq0022jreback
authored andcommitted
split up pandas/tests/indexes/test_multi.py #18644 (#21514)
1 parent 8f32214 commit e77e428

27 files changed

+4552
-3342
lines changed

pandas/tests/indexes/multi/__init__.py

Whitespace-only changes.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
from pandas import Index, MultiIndex
6+
7+
8+
@pytest.fixture
9+
def idx():
10+
# a MultiIndex used to test the general functionality of the
11+
# general functionality of this object
12+
major_axis = Index(['foo', 'bar', 'baz', 'qux'])
13+
minor_axis = Index(['one', 'two'])
14+
15+
major_labels = np.array([0, 0, 1, 2, 3, 3])
16+
minor_labels = np.array([0, 1, 0, 1, 0, 1])
17+
index_names = ['first', 'second']
18+
index = MultiIndex(
19+
levels=[major_axis, minor_axis],
20+
labels=[major_labels, minor_labels],
21+
names=index_names,
22+
verify_integrity=False
23+
)
24+
return index
25+
26+
27+
@pytest.fixture
28+
def index_names():
29+
# names that match those in the idx fixture for testing equality of
30+
# names assigned to the idx
31+
return ['first', 'second']
32+
33+
34+
@pytest.fixture
35+
def holder():
36+
# the MultiIndex constructor used to base compatibility with pickle
37+
return MultiIndex
38+
39+
40+
@pytest.fixture
41+
def compat_props():
42+
# a MultiIndex must have these properties associated with it
43+
return ['shape', 'ndim', 'size']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
4+
def test_shift(idx):
5+
6+
# GH8083 test the base class for shift
7+
pytest.raises(NotImplementedError, idx.shift, 1)
8+
pytest.raises(NotImplementedError, idx.shift, 1, 2)
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
import numpy as np
5+
import pandas.util.testing as tm
6+
import pytest
7+
from pandas import MultiIndex
8+
from pandas.compat import PY3, long
9+
10+
11+
def test_numeric_compat(idx):
12+
tm.assert_raises_regex(TypeError, "cannot perform __mul__",
13+
lambda: idx * 1)
14+
tm.assert_raises_regex(TypeError, "cannot perform __rmul__",
15+
lambda: 1 * idx)
16+
17+
div_err = "cannot perform __truediv__" if PY3 \
18+
else "cannot perform __div__"
19+
tm.assert_raises_regex(TypeError, div_err, lambda: idx / 1)
20+
div_err = div_err.replace(' __', ' __r')
21+
tm.assert_raises_regex(TypeError, div_err, lambda: 1 / idx)
22+
tm.assert_raises_regex(TypeError, "cannot perform __floordiv__",
23+
lambda: idx // 1)
24+
tm.assert_raises_regex(TypeError, "cannot perform __rfloordiv__",
25+
lambda: 1 // idx)
26+
27+
28+
def test_logical_compat(idx):
29+
tm.assert_raises_regex(TypeError, 'cannot perform all',
30+
lambda: idx.all())
31+
tm.assert_raises_regex(TypeError, 'cannot perform any',
32+
lambda: idx.any())
33+
34+
35+
def test_boolean_context_compat(idx):
36+
37+
with pytest.raises(ValueError):
38+
bool(idx)
39+
40+
41+
def test_boolean_context_compat2():
42+
43+
# boolean context compat
44+
# GH7897
45+
i1 = MultiIndex.from_tuples([('A', 1), ('A', 2)])
46+
i2 = MultiIndex.from_tuples([('A', 1), ('A', 3)])
47+
common = i1.intersection(i2)
48+
49+
with pytest.raises(ValueError):
50+
bool(common)
51+
52+
53+
def test_inplace_mutation_resets_values():
54+
levels = [['a', 'b', 'c'], [4]]
55+
levels2 = [[1, 2, 3], ['a']]
56+
labels = [[0, 1, 0, 2, 2, 0], [0, 0, 0, 0, 0, 0]]
57+
58+
mi1 = MultiIndex(levels=levels, labels=labels)
59+
mi2 = MultiIndex(levels=levels2, labels=labels)
60+
vals = mi1.values.copy()
61+
vals2 = mi2.values.copy()
62+
63+
assert mi1._tuples is not None
64+
65+
# Make sure level setting works
66+
new_vals = mi1.set_levels(levels2).values
67+
tm.assert_almost_equal(vals2, new_vals)
68+
69+
# Non-inplace doesn't kill _tuples [implementation detail]
70+
tm.assert_almost_equal(mi1._tuples, vals)
71+
72+
# ...and values is still same too
73+
tm.assert_almost_equal(mi1.values, vals)
74+
75+
# Inplace should kill _tuples
76+
mi1.set_levels(levels2, inplace=True)
77+
tm.assert_almost_equal(mi1.values, vals2)
78+
79+
# Make sure label setting works too
80+
labels2 = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
81+
exp_values = np.empty((6,), dtype=object)
82+
exp_values[:] = [(long(1), 'a')] * 6
83+
84+
# Must be 1d array of tuples
85+
assert exp_values.shape == (6,)
86+
new_values = mi2.set_labels(labels2).values
87+
88+
# Not inplace shouldn't change
89+
tm.assert_almost_equal(mi2._tuples, vals2)
90+
91+
# Should have correct values
92+
tm.assert_almost_equal(exp_values, new_values)
93+
94+
# ...and again setting inplace should kill _tuples, etc
95+
mi2.set_labels(labels2, inplace=True)
96+
tm.assert_almost_equal(mi2.values, new_values)
97+
98+
99+
def test_ndarray_compat_properties(idx, compat_props):
100+
assert idx.T.equals(idx)
101+
assert idx.transpose().equals(idx)
102+
103+
values = idx.values
104+
for prop in compat_props:
105+
assert getattr(idx, prop) == getattr(values, prop)
106+
107+
# test for validity
108+
idx.nbytes
109+
idx.values.nbytes
110+
111+
112+
def test_compat(indices):
113+
assert indices.tolist() == list(indices)
114+
115+
116+
def test_pickle_compat_construction(holder):
117+
# this is testing for pickle compat
118+
if holder is None:
119+
return
120+
121+
# need an object to create with
122+
pytest.raises(TypeError, holder)

0 commit comments

Comments
 (0)