Skip to content

Commit 9bfdb0d

Browse files
authored
REF: implement tests/indexes/objects/ (#31597)
1 parent 2862b3d commit 9bfdb0d

File tree

4 files changed

+110
-95
lines changed

4 files changed

+110
-95
lines changed

pandas/tests/indexes/base_class/__init__.py

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from pandas import Index, MultiIndex
4+
5+
6+
class TestIndexConstructor:
7+
# Tests for the Index constructor, specifically for cases that do
8+
# not return a subclass
9+
10+
def test_constructor_corner(self):
11+
# corner case
12+
msg = (
13+
r"Index\(\.\.\.\) must be called with a collection of some "
14+
"kind, 0 was passed"
15+
)
16+
with pytest.raises(TypeError, match=msg):
17+
Index(0)
18+
19+
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
20+
def test_construction_list_mixed_tuples(self, index_vals):
21+
# see gh-10697: if we are constructing from a mixed list of tuples,
22+
# make sure that we are independent of the sorting order.
23+
index = Index(index_vals)
24+
assert isinstance(index, Index)
25+
assert not isinstance(index, MultiIndex)
26+
27+
def test_constructor_wrong_kwargs(self):
28+
# GH #19348
29+
with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"):
30+
Index([], foo="bar")
31+
32+
@pytest.mark.xfail(reason="see GH#21311: Index doesn't enforce dtype argument")
33+
def test_constructor_cast(self):
34+
msg = "could not convert string to float"
35+
with pytest.raises(ValueError, match=msg):
36+
Index(["a", "b", "c"], dtype=float)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Index, Series
5+
import pandas._testing as tm
6+
from pandas.core.algorithms import safe_sort
7+
8+
9+
class TestIndexSetOps:
10+
def test_union_base(self):
11+
index = Index([0, "a", 1, "b", 2, "c"])
12+
first = index[3:]
13+
second = index[:5]
14+
15+
result = first.union(second)
16+
17+
expected = Index([0, 1, 2, "a", "b", "c"])
18+
tm.assert_index_equal(result, expected)
19+
20+
@pytest.mark.parametrize("klass", [np.array, Series, list])
21+
def test_union_different_type_base(self, klass):
22+
# GH 10149
23+
index = Index([0, "a", 1, "b", 2, "c"])
24+
first = index[3:]
25+
second = index[:5]
26+
27+
result = first.union(klass(second.values))
28+
29+
assert tm.equalContents(result, index)
30+
31+
@pytest.mark.parametrize("sort", [None, False])
32+
def test_intersection_base(self, sort):
33+
# (same results for py2 and py3 but sortedness not tested elsewhere)
34+
index = Index([0, "a", 1, "b", 2, "c"])
35+
first = index[:5]
36+
second = index[:3]
37+
38+
expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])
39+
result = first.intersection(second, sort=sort)
40+
tm.assert_index_equal(result, expected)
41+
42+
@pytest.mark.parametrize("klass", [np.array, Series, list])
43+
@pytest.mark.parametrize("sort", [None, False])
44+
def test_intersection_different_type_base(self, klass, sort):
45+
# GH 10149
46+
index = Index([0, "a", 1, "b", 2, "c"])
47+
first = index[:5]
48+
second = index[:3]
49+
50+
result = first.intersection(klass(second.values), sort=sort)
51+
assert tm.equalContents(result, second)
52+
53+
@pytest.mark.parametrize("sort", [None, False])
54+
def test_difference_base(self, sort):
55+
# (same results for py2 and py3 but sortedness not tested elsewhere)
56+
index = Index([0, "a", 1, "b", 2, "c"])
57+
first = index[:4]
58+
second = index[3:]
59+
60+
result = first.difference(second, sort)
61+
expected = Index([0, "a", 1])
62+
if sort is None:
63+
expected = Index(safe_sort(expected))
64+
tm.assert_index_equal(result, expected)
65+
66+
def test_symmetric_difference(self):
67+
# (same results for py2 and py3 but sortedness not tested elsewhere)
68+
index = Index([0, "a", 1, "b", 2, "c"])
69+
first = index[:4]
70+
second = index[3:]
71+
72+
result = first.symmetric_difference(second)
73+
expected = Index([0, 1, 2, "a", "c"])
74+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/test_base.py

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
period_range,
3535
)
3636
import pandas._testing as tm
37-
from pandas.core.algorithms import safe_sort
3837
from pandas.core.indexes.api import (
3938
Index,
4039
MultiIndex,
@@ -108,23 +107,6 @@ def test_constructor_copy(self, index):
108107
# arr = np.array(5.)
109108
# pytest.raises(Exception, arr.view, Index)
110109

111-
def test_constructor_corner(self):
112-
# corner case
113-
msg = (
114-
r"Index\(\.\.\.\) must be called with a collection of some "
115-
"kind, 0 was passed"
116-
)
117-
with pytest.raises(TypeError, match=msg):
118-
Index(0)
119-
120-
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
121-
def test_construction_list_mixed_tuples(self, index_vals):
122-
# see gh-10697: if we are constructing from a mixed list of tuples,
123-
# make sure that we are independent of the sorting order.
124-
index = Index(index_vals)
125-
assert isinstance(index, Index)
126-
assert not isinstance(index, MultiIndex)
127-
128110
@pytest.mark.parametrize("na_value", [None, np.nan])
129111
@pytest.mark.parametrize("vtype", [list, tuple, iter])
130112
def test_construction_list_tuples_nan(self, na_value, vtype):
@@ -359,11 +341,6 @@ def test_constructor_simple_new(self, vals, dtype):
359341
result = index._simple_new(index.values, dtype)
360342
tm.assert_index_equal(result, index)
361343

362-
def test_constructor_wrong_kwargs(self):
363-
# GH #19348
364-
with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"):
365-
Index([], foo="bar")
366-
367344
@pytest.mark.parametrize(
368345
"vals",
369346
[
@@ -554,12 +531,6 @@ def test_constructor_overflow_int64(self):
554531
with pytest.raises(OverflowError, match=msg):
555532
Index([np.iinfo(np.uint64).max - 1], dtype="int64")
556533

557-
@pytest.mark.xfail(reason="see GH#21311: Index doesn't enforce dtype argument")
558-
def test_constructor_cast(self):
559-
msg = "could not convert string to float"
560-
with pytest.raises(ValueError, match=msg):
561-
Index(["a", "b", "c"], dtype=float)
562-
563534
@pytest.mark.parametrize(
564535
"index",
565536
[
@@ -2528,78 +2499,12 @@ def test_copy_name2(self):
25282499
assert index3.name == "NewName"
25292500
assert index3.names == ["NewName"]
25302501

2531-
def test_union_base(self):
2532-
index = self.create_index()
2533-
first = index[3:]
2534-
second = index[:5]
2535-
2536-
result = first.union(second)
2537-
2538-
expected = Index([0, 1, 2, "a", "b", "c"])
2539-
tm.assert_index_equal(result, expected)
2540-
2541-
@pytest.mark.parametrize("klass", [np.array, Series, list])
2542-
def test_union_different_type_base(self, klass):
2543-
# GH 10149
2544-
index = self.create_index()
2545-
first = index[3:]
2546-
second = index[:5]
2547-
2548-
result = first.union(klass(second.values))
2549-
2550-
assert tm.equalContents(result, index)
2551-
25522502
def test_unique_na(self):
25532503
idx = pd.Index([2, np.nan, 2, 1], name="my_index")
25542504
expected = pd.Index([2, np.nan, 1], name="my_index")
25552505
result = idx.unique()
25562506
tm.assert_index_equal(result, expected)
25572507

2558-
@pytest.mark.parametrize("sort", [None, False])
2559-
def test_intersection_base(self, sort):
2560-
# (same results for py2 and py3 but sortedness not tested elsewhere)
2561-
index = self.create_index()
2562-
first = index[:5]
2563-
second = index[:3]
2564-
2565-
expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])
2566-
result = first.intersection(second, sort=sort)
2567-
tm.assert_index_equal(result, expected)
2568-
2569-
@pytest.mark.parametrize("klass", [np.array, Series, list])
2570-
@pytest.mark.parametrize("sort", [None, False])
2571-
def test_intersection_different_type_base(self, klass, sort):
2572-
# GH 10149
2573-
index = self.create_index()
2574-
first = index[:5]
2575-
second = index[:3]
2576-
2577-
result = first.intersection(klass(second.values), sort=sort)
2578-
assert tm.equalContents(result, second)
2579-
2580-
@pytest.mark.parametrize("sort", [None, False])
2581-
def test_difference_base(self, sort):
2582-
# (same results for py2 and py3 but sortedness not tested elsewhere)
2583-
index = self.create_index()
2584-
first = index[:4]
2585-
second = index[3:]
2586-
2587-
result = first.difference(second, sort)
2588-
expected = Index([0, "a", 1])
2589-
if sort is None:
2590-
expected = Index(safe_sort(expected))
2591-
tm.assert_index_equal(result, expected)
2592-
2593-
def test_symmetric_difference(self):
2594-
# (same results for py2 and py3 but sortedness not tested elsewhere)
2595-
index = self.create_index()
2596-
first = index[:4]
2597-
second = index[3:]
2598-
2599-
result = first.symmetric_difference(second)
2600-
expected = Index([0, 1, 2, "a", "c"])
2601-
tm.assert_index_equal(result, expected)
2602-
26032508
def test_logical_compat(self):
26042509
index = self.create_index()
26052510
assert index.all() == index.values.all()

0 commit comments

Comments
 (0)