20
20
21
21
22
22
class TestSlicing :
23
+ def test_return_type_doesnt_depend_on_monotonicity (self ):
24
+ # GH#24892 we get Series back regardless of whether our DTI is monotonic
25
+ dti = date_range (start = "2015-5-13 23:59:00" , freq = "min" , periods = 3 )
26
+ ser = Series (range (3 ), index = dti )
27
+
28
+ # non-monotonic index
29
+ ser2 = Series (range (3 ), index = [dti [1 ], dti [0 ], dti [2 ]])
30
+
31
+ # key with resolution strictly lower than "min"
32
+ key = "2015-5-14 00"
33
+
34
+ # monotonic increasing index
35
+ result = ser .loc [key ]
36
+ expected = ser .iloc [1 :]
37
+ tm .assert_series_equal (result , expected )
38
+
39
+ # monotonic decreasing index
40
+ result = ser .iloc [::- 1 ].loc [key ]
41
+ expected = ser .iloc [::- 1 ][:- 1 ]
42
+ tm .assert_series_equal (result , expected )
43
+
44
+ # non-monotonic index
45
+ result2 = ser2 .loc [key ]
46
+ expected2 = ser2 .iloc [::2 ]
47
+ tm .assert_series_equal (result2 , expected2 )
48
+
49
+ def test_return_type_doesnt_depend_on_monotonicity_higher_reso (self ):
50
+ # GH#24892 we get Series back regardless of whether our DTI is monotonic
51
+ dti = date_range (start = "2015-5-13 23:59:00" , freq = "min" , periods = 3 )
52
+ ser = Series (range (3 ), index = dti )
53
+
54
+ # non-monotonic index
55
+ ser2 = Series (range (3 ), index = [dti [1 ], dti [0 ], dti [2 ]])
56
+
57
+ # key with resolution strictly *higher) than "min"
58
+ key = "2015-5-14 00:00:00"
59
+
60
+ # monotonic increasing index
61
+ result = ser .loc [key ]
62
+ assert result == 1
63
+
64
+ # monotonic decreasing index
65
+ result = ser .iloc [::- 1 ].loc [key ]
66
+ assert result == 1
67
+
68
+ # non-monotonic index
69
+ result2 = ser2 .loc [key ]
70
+ assert result2 == 0
71
+
23
72
def test_monotone_DTI_indexing_bug (self ):
24
73
# GH 19362
25
74
# Testing accessing the first element in a monotonic descending
@@ -38,9 +87,19 @@ def test_monotone_DTI_indexing_bug(self):
38
87
expected = DataFrame ({0 : list (range (5 )), "date" : date_index })
39
88
tm .assert_frame_equal (df , expected )
40
89
41
- df = DataFrame ({"A" : [1 , 2 , 3 ]}, index = date_range ("20170101" , periods = 3 )[::- 1 ])
42
- expected = DataFrame ({"A" : 1 }, index = date_range ("20170103" , periods = 1 )[::- 1 ])
43
- tm .assert_frame_equal (df .loc ["2017-01-03" ], expected )
90
+ # We get a slice because df.index's resolution is hourly and we
91
+ # are slicing with a daily-resolution string. If both were daily,
92
+ # we would get a single item back
93
+ dti = date_range ("20170101 01:00:00" , periods = 3 )
94
+ df = DataFrame ({"A" : [1 , 2 , 3 ]}, index = dti [::- 1 ])
95
+
96
+ expected = DataFrame ({"A" : 1 }, index = dti [- 1 :][::- 1 ])
97
+ result = df .loc ["2017-01-03" ]
98
+ tm .assert_frame_equal (result , expected )
99
+
100
+ result2 = df .iloc [::- 1 ].loc ["2017-01-03" ]
101
+ expected2 = expected .iloc [::- 1 ]
102
+ tm .assert_frame_equal (result2 , expected2 )
44
103
45
104
def test_slice_year (self ):
46
105
dti = date_range (freq = "B" , start = datetime (2005 , 1 , 1 ), periods = 500 )
0 commit comments