Skip to content

Commit 436852e

Browse files
committed
Review related changes
1 parent 91eecef commit 436852e

File tree

6 files changed

+71
-56
lines changed

6 files changed

+71
-56
lines changed

pandas/tests/indexes/common.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def test_difference_base(self):
632632
pass
633633
elif isinstance(idx, (DatetimeIndex, TimedeltaIndex)):
634634
assert result.__class__ == answer.__class__
635-
tm.assert_numpy_array_equal(result.asi8, answer.asi8)
635+
assert tm.equalContents(result.asi8, answer.asi8)
636636
else:
637637
result = first.difference(case)
638638
assert tm.equalContents(result, answer)
@@ -954,3 +954,24 @@ def test_join_self_unique(self, how):
954954
if index.is_unique:
955955
joined = index.join(index, how=how)
956956
assert (index == joined).all()
957+
958+
def test_searchsorted_monotonic(self):
959+
# GH17271
960+
for index in self.indices.values():
961+
# not implemented for tuple searches in MultiIndex
962+
# or Intervals searches in IntervalIndex
963+
if isinstance(index, (MultiIndex, IntervalIndex)):
964+
continue
965+
966+
# nothing to test if the index is empty
967+
if index.empty:
968+
continue
969+
value = index[0]
970+
971+
if index.is_monotonic_increasing or index.is_monotonic_decreasing:
972+
assert index._searchsorted_monotonic(value, side='left') == 0
973+
assert index._searchsorted_monotonic(value, side='right') == 1
974+
else:
975+
# non-monotonic should raise.
976+
with pytest.raises(ValueError):
977+
index._searchsorted_monotonic(value, side='left')

pandas/tests/indexes/datetimes/test_datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class TestDatetimeIndex(DatetimeLike):
1212
_holder = DatetimeIndex
1313

1414
def setup_method(self, method):
15-
self.indices = dict(index=tm.makeDateIndex(10))
15+
self.indices = dict(index=tm.makeDateIndex(10),
16+
index_dec=date_range('20130110', periods=10,
17+
freq='-1D'))
1618
self.setup_indices()
1719

1820
def create_index(self):

pandas/tests/indexes/period/test_period.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class TestPeriodIndex(DatetimeLike):
1818
_multiprocess_can_split_ = True
1919

2020
def setup_method(self, method):
21-
self.indices = dict(index=tm.makePeriodIndex(10))
21+
self.indices = dict(index=tm.makePeriodIndex(10),
22+
index_dec=period_range('20130101', periods=10,
23+
freq='D')[::-1])
2224
self.setup_indices()
2325

2426
def create_index(self):

pandas/tests/indexes/test_base.py

-21
Original file line numberDiff line numberDiff line change
@@ -2103,24 +2103,3 @@ def test_intersect_str_dates(self):
21032103
res = i2.intersection(i1)
21042104

21052105
assert len(res) == 0
2106-
2107-
def test_searchsorted_monotonic(self):
2108-
# GH17271
2109-
idx_inc = Index([0, 2, 4])
2110-
idx_dec = Index([4, 2, 0])
2111-
2112-
# test included value.
2113-
assert idx_inc._searchsorted_monotonic(0, side='left') == 0
2114-
assert idx_inc._searchsorted_monotonic(0, side='right') == 1
2115-
assert idx_dec._searchsorted_monotonic(0, side='left') == 2
2116-
assert idx_dec._searchsorted_monotonic(0, side='right') == 3
2117-
2118-
# test non-included value.
2119-
for side in ('left', 'right'):
2120-
assert idx_inc._searchsorted_monotonic(1, side=side) == 1
2121-
assert idx_dec._searchsorted_monotonic(1, side=side) == 2
2122-
2123-
# non-monotonic should raise.
2124-
idx_bad = Index([0, 4, 2])
2125-
with pytest.raises(ValueError):
2126-
idx_bad._searchsorted_monotonic(3)

pandas/tests/indexes/test_numeric.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ class TestFloat64Index(Numeric):
181181

182182
def setup_method(self, method):
183183
self.indices = dict(mixed=Float64Index([1.5, 2, 3, 4, 5]),
184-
float=Float64Index(np.arange(5) * 2.5))
184+
float=Float64Index(np.arange(5) * 2.5),
185+
mixed_dec=Float64Index([5, 4, 3, 2, 1.5]),
186+
float_dec=Float64Index(np.arange(4, -1, -1) * 2.5))
185187
self.setup_indices()
186188

187189
def create_index(self):
@@ -654,7 +656,8 @@ class TestInt64Index(NumericInt):
654656
_holder = Int64Index
655657

656658
def setup_method(self, method):
657-
self.indices = dict(index=Int64Index(np.arange(0, 20, 2)))
659+
self.indices = dict(index=Int64Index(np.arange(0, 20, 2)),
660+
index_dec=Int64Index(np.arange(19, -1, -1)))
658661
self.setup_indices()
659662

660663
def create_index(self):
@@ -949,8 +952,9 @@ class TestUInt64Index(NumericInt):
949952
_holder = UInt64Index
950953

951954
def setup_method(self, method):
952-
self.indices = dict(index=UInt64Index([2**63, 2**63 + 10, 2**63 + 15,
953-
2**63 + 20, 2**63 + 25]))
955+
vals = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
956+
self.indices = dict(index=UInt64Index(vals),
957+
index_dec=UInt64Index(reversed(vals)))
954958
self.setup_indices()
955959

956960
def create_index(self):

pandas/tests/indexing/test_interval.py

+35-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pandas as pd
44

55
from pandas import Series, DataFrame, IntervalIndex, Interval
6+
from pandas.compat import product
67
import pandas.util.testing as tm
78

89

@@ -11,22 +12,9 @@ class TestIntervalIndex(object):
1112
def setup_method(self, method):
1213
self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6)))
1314

14-
idx_dec = IntervalIndex.from_tuples([(2, 3), (1, 2), (0, 1)])
15-
self.s_dec = Series(list('abc'), idx_dec)
16-
1715
def test_loc_with_scalar(self):
1816

1917
s = self.s
20-
expected = 0
21-
22-
result = s.loc[0.5]
23-
assert result == expected
24-
25-
result = s.loc[1]
26-
assert result == expected
27-
28-
with pytest.raises(KeyError):
29-
s.loc[0]
3018

3119
expected = s.iloc[:3]
3220
tm.assert_series_equal(expected, s.loc[:3])
@@ -42,22 +30,9 @@ def test_loc_with_scalar(self):
4230
expected = s.iloc[2:5]
4331
tm.assert_series_equal(expected, s.loc[s >= 2])
4432

45-
# test endpoint of non-overlapping monotonic decreasing (GH16417)
46-
assert self.s_dec.loc[3] == 'a'
47-
4833
def test_getitem_with_scalar(self):
4934

5035
s = self.s
51-
expected = 0
52-
53-
result = s[0.5]
54-
assert result == expected
55-
56-
result = s[1]
57-
assert result == expected
58-
59-
with pytest.raises(KeyError):
60-
s[0]
6136

6237
expected = s.iloc[:3]
6338
tm.assert_series_equal(expected, s[:3])
@@ -73,8 +48,40 @@ def test_getitem_with_scalar(self):
7348
expected = s.iloc[2:5]
7449
tm.assert_series_equal(expected, s[s >= 2])
7550

76-
# test endpoint of non-overlapping monotonic decreasing (GH16417)
77-
assert self.s_dec[3] == 'a'
51+
@pytest.mark.parametrize('direction, closed',
52+
product(('increasing', 'decreasing'),
53+
('left', 'right', 'neither', 'both')))
54+
def test_nonoverlapping_monotonic(self, direction, closed):
55+
tpls = [(0, 1), (2, 3), (4, 5)]
56+
if direction == 'decreasing':
57+
tpls = reversed(tpls)
58+
59+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
60+
s = Series(list('abc'), idx)
61+
62+
for key, expected in zip(idx.left, s):
63+
if idx.closed_left:
64+
assert s[key] == expected
65+
assert s.loc[key] == expected
66+
else:
67+
with pytest.raises(KeyError):
68+
s[key]
69+
with pytest.raises(KeyError):
70+
s.loc[key]
71+
72+
for key, expected in zip(idx.right, s):
73+
if idx.closed_right:
74+
assert s[key] == expected
75+
assert s.loc[key] == expected
76+
else:
77+
with pytest.raises(KeyError):
78+
s[key]
79+
with pytest.raises(KeyError):
80+
s.loc[key]
81+
82+
for key, expected in zip(idx.mid, s):
83+
assert s[key] == expected
84+
assert s.loc[key] == expected
7885

7986
def test_with_interval(self):
8087

0 commit comments

Comments
 (0)