21
21
)
22
22
import pandas ._testing as tm
23
23
24
- from pandas .tseries .offsets import (
25
- BDay ,
26
- CDay ,
27
- )
24
+ from pandas .tseries .frequencies import to_offset
28
25
29
26
START , END = datetime (2009 , 1 , 1 ), datetime (2010 , 1 , 1 )
30
27
31
28
32
29
class TestGetItem :
33
- def test_ellipsis (self ):
34
- # GH#21282
35
- idx = date_range (
36
- "2011-01-01" , "2011-01-31" , freq = "D" , tz = "Asia/Tokyo" , name = "idx"
37
- )
38
-
39
- result = idx [...]
40
- assert result .equals (idx )
41
- assert result is not idx
42
-
43
30
def test_getitem_slice_keeps_name (self ):
44
31
# GH4226
45
32
st = Timestamp ("2013-07-01 00:00:00" , tz = "America/Los_Angeles" )
@@ -88,44 +75,17 @@ def test_getitem(self):
88
75
tm .assert_index_equal (result , expected )
89
76
assert result .freq == expected .freq
90
77
91
- def test_dti_business_getitem (self ):
92
- rng = bdate_range (START , END )
93
- smaller = rng [:5 ]
94
- exp = DatetimeIndex (rng .view (np .ndarray )[:5 ], freq = "B" )
95
- tm .assert_index_equal (smaller , exp )
96
- assert smaller .freq == exp .freq
97
-
98
- assert smaller .freq == rng .freq
99
-
100
- sliced = rng [::5 ]
101
- assert sliced .freq == BDay () * 5
102
-
103
- fancy_indexed = rng [[4 , 3 , 2 , 1 , 0 ]]
104
- assert len (fancy_indexed ) == 5
105
- assert isinstance (fancy_indexed , DatetimeIndex )
106
- assert fancy_indexed .freq is None
107
-
108
- # 32-bit vs. 64-bit platforms
109
- assert rng [4 ] == rng [np .int_ (4 )]
110
-
111
- def test_dti_business_getitem_matplotlib_hackaround (self ):
112
- rng = bdate_range (START , END )
113
- with tm .assert_produces_warning (FutureWarning ):
114
- # GH#30588 multi-dimensional indexing deprecated
115
- values = rng [:, None ]
116
- expected = rng .values [:, None ]
117
- tm .assert_numpy_array_equal (values , expected )
118
-
119
- def test_dti_custom_getitem (self ):
120
- rng = bdate_range (START , END , freq = "C" )
78
+ @pytest .mark .parametrize ("freq" , ["B" , "C" ])
79
+ def test_dti_business_getitem (self , freq ):
80
+ rng = bdate_range (START , END , freq = freq )
121
81
smaller = rng [:5 ]
122
- exp = DatetimeIndex (rng .view (np .ndarray )[:5 ], freq = "C" )
82
+ exp = DatetimeIndex (rng .view (np .ndarray )[:5 ], freq = freq )
123
83
tm .assert_index_equal (smaller , exp )
124
84
assert smaller .freq == exp .freq
125
85
assert smaller .freq == rng .freq
126
86
127
87
sliced = rng [::5 ]
128
- assert sliced .freq == CDay ( ) * 5
88
+ assert sliced .freq == to_offset ( freq ) * 5
129
89
130
90
fancy_indexed = rng [[4 , 3 , 2 , 1 , 0 ]]
131
91
assert len (fancy_indexed ) == 5
@@ -135,8 +95,9 @@ def test_dti_custom_getitem(self):
135
95
# 32-bit vs. 64-bit platforms
136
96
assert rng [4 ] == rng [np .int_ (4 )]
137
97
138
- def test_dti_custom_getitem_matplotlib_hackaround (self ):
139
- rng = bdate_range (START , END , freq = "C" )
98
+ @pytest .mark .parametrize ("freq" , ["B" , "C" ])
99
+ def test_dti_business_getitem_matplotlib_hackaround (self , freq ):
100
+ rng = bdate_range (START , END , freq = freq )
140
101
with tm .assert_produces_warning (FutureWarning ):
141
102
# GH#30588 multi-dimensional indexing deprecated
142
103
values = rng [:, None ]
@@ -255,6 +216,12 @@ def test_where_tz(self):
255
216
256
217
257
218
class TestTake :
219
+ def test_take_nan_first_datetime (self ):
220
+ index = DatetimeIndex ([pd .NaT , Timestamp ("20130101" ), Timestamp ("20130102" )])
221
+ result = index .take ([- 1 , 0 , 1 ])
222
+ expected = DatetimeIndex ([index [- 1 ], index [0 ], index [1 ]])
223
+ tm .assert_index_equal (result , expected )
224
+
258
225
def test_take (self ):
259
226
# GH#10295
260
227
idx1 = date_range ("2011-01-01" , "2011-01-31" , freq = "D" , name = "idx" )
0 commit comments