Skip to content

Commit 628ccf8

Browse files
committed
Test searchsorted and misc changes
1 parent ec519df commit 628ccf8

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

pandas/tests/indexes/common.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ def test_get_indexer_consistency(self):
138138
if isinstance(index, IntervalIndex):
139139
continue
140140

141-
indexer = index.get_indexer(index[0:2])
142-
assert isinstance(indexer, np.ndarray)
143-
assert indexer.dtype == np.intp
141+
if index.is_unique:
142+
indexer = index.get_indexer(index[0:2])
143+
assert isinstance(indexer, np.ndarray)
144+
assert indexer.dtype == np.intp
144145

145146
indexer, _ = index.get_indexer_non_unique(index[0:2])
146147
assert isinstance(indexer, np.ndarray)
@@ -551,8 +552,6 @@ def test_setops_errorcases(self):
551552
"or array-like",
552553
method, case)
553554

554-
@pytest.mark.xfail(reason='intersection fails for monotonic decreasing '
555-
'RangeIndex (GH 17296)')
556555
def test_intersection_base(self):
557556
for name, idx in compat.iteritems(self.indices):
558557
first = idx[:5]
@@ -561,6 +560,9 @@ def test_intersection_base(self):
561560

562561
if isinstance(idx, CategoricalIndex):
563562
pass
563+
elif isinstance(idx, RangeIndex):
564+
pytest.xfail(reason='intersection fails for decreasing '
565+
'RangeIndex (GH 17296)')
564566
else:
565567
assert tm.equalContents(intersect, second)
566568

@@ -971,9 +973,32 @@ def test_searchsorted_monotonic(self):
971973
continue
972974
value = index[0]
973975

974-
if index.is_monotonic_increasing or index.is_monotonic_decreasing:
975-
assert index._searchsorted_monotonic(value, side='left') == 0
976-
assert index._searchsorted_monotonic(value, side='right') == 1
976+
# determine the expected results (handle dupes for 'right')
977+
expected_left, expected_right = 0, (index == value).argmin()
978+
if expected_right == 0:
979+
# all values are the same, expected_right should be length
980+
expected_right = len(index)
981+
982+
# test _searchsorted_monotonic in all cases
983+
# test searchsorted only for increasing
984+
if index.is_monotonic_increasing:
985+
ssm_left = index._searchsorted_monotonic(value, side='left')
986+
assert expected_left == ssm_left
987+
988+
ssm_right = index._searchsorted_monotonic(value, side='right')
989+
assert expected_right == ssm_right
990+
991+
ss_left = index.searchsorted(value, side='left')
992+
assert expected_left == ss_left
993+
994+
ss_right = index.searchsorted(value, side='right')
995+
assert expected_right == ss_right
996+
elif index.is_monotonic_decreasing:
997+
ssm_left = index._searchsorted_monotonic(value, side='left')
998+
assert expected_left == ssm_left
999+
1000+
ssm_right = index._searchsorted_monotonic(value, side='right')
1001+
assert expected_right == ssm_right
9771002
else:
9781003
# non-monotonic should raise.
9791004
with pytest.raises(ValueError):

pandas/tests/indexes/test_base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def setup_method(self, method):
4646
catIndex=tm.makeCategoricalIndex(100),
4747
empty=Index([]),
4848
tuples=MultiIndex.from_tuples(lzip(
49-
['foo', 'bar', 'baz'], [1, 2, 3])))
49+
['foo', 'bar', 'baz'], [1, 2, 3])),
50+
repeats=Index([0, 0, 1, 1, 2, 2]))
5051
self.setup_indices()
5152

5253
def create_index(self):

0 commit comments

Comments
 (0)