Skip to content

Commit 1b2cb6a

Browse files
jbrockmendelroberthdevries
authored andcommitted
REF: organize base class Index tests (pandas-dev#31864)
1 parent 8f59cd8 commit 1b2cb6a

File tree

6 files changed

+185
-178
lines changed

6 files changed

+185
-178
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Tests for ndarray-like method on the base Index class
3+
"""
4+
import pytest
5+
6+
import pandas as pd
7+
from pandas import Index
8+
import pandas._testing as tm
9+
10+
11+
class TestReshape:
12+
def test_repeat(self):
13+
repeats = 2
14+
index = pd.Index([1, 2, 3])
15+
expected = pd.Index([1, 1, 2, 2, 3, 3])
16+
17+
result = index.repeat(repeats)
18+
tm.assert_index_equal(result, expected)
19+
20+
def test_insert(self):
21+
22+
# GH 7256
23+
# validate neg/pos inserts
24+
result = Index(["b", "c", "d"])
25+
26+
# test 0th element
27+
tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))
28+
29+
# test Nth element that follows Python list behavior
30+
tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))
31+
32+
# test loc +/- neq (0, -1)
33+
tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))
34+
35+
# test empty
36+
null_index = Index([])
37+
tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))
38+
39+
@pytest.mark.parametrize(
40+
"pos,expected",
41+
[
42+
(0, Index(["b", "c", "d"], name="index")),
43+
(-1, Index(["a", "b", "c"], name="index")),
44+
],
45+
)
46+
def test_delete(self, pos, expected):
47+
index = Index(["a", "b", "c", "d"], name="index")
48+
result = index.delete(pos)
49+
tm.assert_index_equal(result, expected)
50+
assert result.name == expected.name
51+
52+
def test_append_multiple(self):
53+
index = Index(["a", "b", "c", "d", "e", "f"])
54+
55+
foos = [index[:2], index[2:4], index[4:]]
56+
result = foos[0].append(foos[1:])
57+
tm.assert_index_equal(result, index)
58+
59+
# empty
60+
result = index.append([])
61+
tm.assert_index_equal(result, index)

pandas/tests/indexes/base_class/test_setops.py

+73
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
import numpy as np
22
import pytest
33

4+
import pandas as pd
45
from pandas import Index, Series
56
import pandas._testing as tm
67
from pandas.core.algorithms import safe_sort
78

89

910
class TestIndexSetOps:
11+
@pytest.mark.parametrize(
12+
"method", ["union", "intersection", "difference", "symmetric_difference"]
13+
)
14+
def test_setops_disallow_true(self, method):
15+
idx1 = pd.Index(["a", "b"])
16+
idx2 = pd.Index(["b", "c"])
17+
18+
with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
19+
getattr(idx1, method)(idx2, sort=True)
20+
21+
def test_setops_preserve_object_dtype(self):
22+
idx = pd.Index([1, 2, 3], dtype=object)
23+
result = idx.intersection(idx[1:])
24+
expected = idx[1:]
25+
tm.assert_index_equal(result, expected)
26+
27+
# if other is not monotonic increasing, intersection goes through
28+
# a different route
29+
result = idx.intersection(idx[1:][::-1])
30+
tm.assert_index_equal(result, expected)
31+
32+
result = idx._union(idx[1:], sort=None)
33+
expected = idx
34+
tm.assert_index_equal(result, expected)
35+
36+
result = idx.union(idx[1:], sort=None)
37+
tm.assert_index_equal(result, expected)
38+
39+
# if other is not monotonic increasing, _union goes through
40+
# a different route
41+
result = idx._union(idx[1:][::-1], sort=None)
42+
tm.assert_index_equal(result, expected)
43+
44+
result = idx.union(idx[1:][::-1], sort=None)
45+
tm.assert_index_equal(result, expected)
46+
1047
def test_union_base(self):
1148
index = Index([0, "a", 1, "b", 2, "c"])
1249
first = index[3:]
@@ -28,6 +65,32 @@ def test_union_different_type_base(self, klass):
2865

2966
assert tm.equalContents(result, index)
3067

68+
def test_union_sort_other_incomparable(self):
69+
# https://github.com/pandas-dev/pandas/issues/24959
70+
idx = pd.Index([1, pd.Timestamp("2000")])
71+
# default (sort=None)
72+
with tm.assert_produces_warning(RuntimeWarning):
73+
result = idx.union(idx[:1])
74+
75+
tm.assert_index_equal(result, idx)
76+
77+
# sort=None
78+
with tm.assert_produces_warning(RuntimeWarning):
79+
result = idx.union(idx[:1], sort=None)
80+
tm.assert_index_equal(result, idx)
81+
82+
# sort=False
83+
result = idx.union(idx[:1], sort=False)
84+
tm.assert_index_equal(result, idx)
85+
86+
@pytest.mark.xfail(reason="Not implemented")
87+
def test_union_sort_other_incomparable_true(self):
88+
# TODO decide on True behaviour
89+
# sort=True
90+
idx = pd.Index([1, pd.Timestamp("2000")])
91+
with pytest.raises(TypeError, match=".*"):
92+
idx.union(idx[:1], sort=True)
93+
3194
@pytest.mark.parametrize("sort", [None, False])
3295
def test_intersection_base(self, sort):
3396
# (same results for py2 and py3 but sortedness not tested elsewhere)
@@ -50,6 +113,16 @@ def test_intersection_different_type_base(self, klass, sort):
50113
result = first.intersection(klass(second.values), sort=sort)
51114
assert tm.equalContents(result, second)
52115

116+
def test_intersect_nosort(self):
117+
result = pd.Index(["c", "b", "a"]).intersection(["b", "a"])
118+
expected = pd.Index(["b", "a"])
119+
tm.assert_index_equal(result, expected)
120+
121+
def test_intersection_equal_sort(self):
122+
idx = pd.Index(["c", "a", "b"])
123+
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
124+
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)
125+
53126
@pytest.mark.parametrize("sort", [None, False])
54127
def test_difference_base(self, sort):
55128
# (same results for py2 and py3 but sortedness not tested elsewhere)

pandas/tests/indexes/test_any_index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88

99
def test_sort(indices):
10-
with pytest.raises(TypeError):
10+
msg = "cannot sort an Index object in-place, use sort_values instead"
11+
with pytest.raises(TypeError, match=msg):
1112
indices.sort()
1213

1314

0 commit comments

Comments
 (0)