3
3
import pandas as pd
4
4
5
5
from pandas import Series , DataFrame , IntervalIndex , Interval
6
+ from pandas .compat import product
6
7
import pandas .util .testing as tm
7
8
8
9
@@ -11,22 +12,9 @@ class TestIntervalIndex(object):
11
12
def setup_method (self , method ):
12
13
self .s = Series (np .arange (5 ), IntervalIndex .from_breaks (np .arange (6 )))
13
14
14
- idx_dec = IntervalIndex .from_tuples ([(2 , 3 ), (1 , 2 ), (0 , 1 )])
15
- self .s_dec = Series (list ('abc' ), idx_dec )
16
-
17
15
def test_loc_with_scalar (self ):
18
16
19
17
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 ]
30
18
31
19
expected = s .iloc [:3 ]
32
20
tm .assert_series_equal (expected , s .loc [:3 ])
@@ -42,22 +30,9 @@ def test_loc_with_scalar(self):
42
30
expected = s .iloc [2 :5 ]
43
31
tm .assert_series_equal (expected , s .loc [s >= 2 ])
44
32
45
- # test endpoint of non-overlapping monotonic decreasing (GH16417)
46
- assert self .s_dec .loc [3 ] == 'a'
47
-
48
33
def test_getitem_with_scalar (self ):
49
34
50
35
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 ]
61
36
62
37
expected = s .iloc [:3 ]
63
38
tm .assert_series_equal (expected , s [:3 ])
@@ -73,8 +48,40 @@ def test_getitem_with_scalar(self):
73
48
expected = s .iloc [2 :5 ]
74
49
tm .assert_series_equal (expected , s [s >= 2 ])
75
50
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
78
85
79
86
def test_with_interval (self ):
80
87
0 commit comments