@@ -65,6 +65,72 @@ def test_timestamp_invalid_key(self, key):
65
65
tdi .get_loc (key )
66
66
67
67
68
+ class TestGetLoc :
69
+ def test_get_loc (self ):
70
+ idx = pd .to_timedelta (["0 days" , "1 days" , "2 days" ])
71
+
72
+ for method in [None , "pad" , "backfill" , "nearest" ]:
73
+ assert idx .get_loc (idx [1 ], method ) == 1
74
+ assert idx .get_loc (idx [1 ].to_pytimedelta (), method ) == 1
75
+ assert idx .get_loc (str (idx [1 ]), method ) == 1
76
+
77
+ assert idx .get_loc (idx [1 ], "pad" , tolerance = Timedelta (0 )) == 1
78
+ assert idx .get_loc (idx [1 ], "pad" , tolerance = np .timedelta64 (0 , "s" )) == 1
79
+ assert idx .get_loc (idx [1 ], "pad" , tolerance = timedelta (0 )) == 1
80
+
81
+ with pytest .raises (ValueError , match = "unit abbreviation w/o a number" ):
82
+ idx .get_loc (idx [1 ], method = "nearest" , tolerance = "foo" )
83
+
84
+ with pytest .raises (ValueError , match = "tolerance size must match" ):
85
+ idx .get_loc (
86
+ idx [1 ],
87
+ method = "nearest" ,
88
+ tolerance = [
89
+ Timedelta (0 ).to_timedelta64 (),
90
+ Timedelta (0 ).to_timedelta64 (),
91
+ ],
92
+ )
93
+
94
+ for method , loc in [("pad" , 1 ), ("backfill" , 2 ), ("nearest" , 1 )]:
95
+ assert idx .get_loc ("1 day 1 hour" , method ) == loc
96
+
97
+ # GH 16909
98
+ assert idx .get_loc (idx [1 ].to_timedelta64 ()) == 1
99
+
100
+ # GH 16896
101
+ assert idx .get_loc ("0 days" ) == 0
102
+
103
+ def test_get_loc_nat (self ):
104
+ tidx = TimedeltaIndex (["1 days 01:00:00" , "NaT" , "2 days 01:00:00" ])
105
+
106
+ assert tidx .get_loc (pd .NaT ) == 1
107
+ assert tidx .get_loc (None ) == 1
108
+ assert tidx .get_loc (float ("nan" )) == 1
109
+ assert tidx .get_loc (np .nan ) == 1
110
+
111
+
112
+ class TestGetIndexer :
113
+ def test_get_indexer (self ):
114
+ idx = pd .to_timedelta (["0 days" , "1 days" , "2 days" ])
115
+ tm .assert_numpy_array_equal (
116
+ idx .get_indexer (idx ), np .array ([0 , 1 , 2 ], dtype = np .intp )
117
+ )
118
+
119
+ target = pd .to_timedelta (["-1 hour" , "12 hours" , "1 day 1 hour" ])
120
+ tm .assert_numpy_array_equal (
121
+ idx .get_indexer (target , "pad" ), np .array ([- 1 , 0 , 1 ], dtype = np .intp )
122
+ )
123
+ tm .assert_numpy_array_equal (
124
+ idx .get_indexer (target , "backfill" ), np .array ([0 , 1 , 2 ], dtype = np .intp )
125
+ )
126
+ tm .assert_numpy_array_equal (
127
+ idx .get_indexer (target , "nearest" ), np .array ([0 , 1 , 1 ], dtype = np .intp )
128
+ )
129
+
130
+ res = idx .get_indexer (target , "nearest" , tolerance = Timedelta ("1 hour" ))
131
+ tm .assert_numpy_array_equal (res , np .array ([0 , - 1 , 1 ], dtype = np .intp ))
132
+
133
+
68
134
class TestWhere :
69
135
def test_where_doesnt_retain_freq (self ):
70
136
tdi = timedelta_range ("1 day" , periods = 3 , freq = "D" , name = "idx" )
@@ -187,124 +253,3 @@ def test_take_fill_value(self):
187
253
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
188
254
with pytest .raises (IndexError , match = msg ):
189
255
idx .take (np .array ([1 , - 5 ]))
190
-
191
-
192
- class TestTimedeltaIndex :
193
- def test_delete (self ):
194
- idx = timedelta_range (start = "1 Days" , periods = 5 , freq = "D" , name = "idx" )
195
-
196
- # preserve freq
197
- expected_0 = timedelta_range (start = "2 Days" , periods = 4 , freq = "D" , name = "idx" )
198
- expected_4 = timedelta_range (start = "1 Days" , periods = 4 , freq = "D" , name = "idx" )
199
-
200
- # reset freq to None
201
- expected_1 = TimedeltaIndex (
202
- ["1 day" , "3 day" , "4 day" , "5 day" ], freq = None , name = "idx"
203
- )
204
-
205
- cases = {
206
- 0 : expected_0 ,
207
- - 5 : expected_0 ,
208
- - 1 : expected_4 ,
209
- 4 : expected_4 ,
210
- 1 : expected_1 ,
211
- }
212
- for n , expected in cases .items ():
213
- result = idx .delete (n )
214
- tm .assert_index_equal (result , expected )
215
- assert result .name == expected .name
216
- assert result .freq == expected .freq
217
-
218
- with pytest .raises ((IndexError , ValueError )):
219
- # either depending on numpy version
220
- idx .delete (5 )
221
-
222
- def test_delete_slice (self ):
223
- idx = timedelta_range (start = "1 days" , periods = 10 , freq = "D" , name = "idx" )
224
-
225
- # preserve freq
226
- expected_0_2 = timedelta_range (start = "4 days" , periods = 7 , freq = "D" , name = "idx" )
227
- expected_7_9 = timedelta_range (start = "1 days" , periods = 7 , freq = "D" , name = "idx" )
228
-
229
- # reset freq to None
230
- expected_3_5 = TimedeltaIndex (
231
- ["1 d" , "2 d" , "3 d" , "7 d" , "8 d" , "9 d" , "10d" ], freq = None , name = "idx"
232
- )
233
-
234
- cases = {
235
- (0 , 1 , 2 ): expected_0_2 ,
236
- (7 , 8 , 9 ): expected_7_9 ,
237
- (3 , 4 , 5 ): expected_3_5 ,
238
- }
239
- for n , expected in cases .items ():
240
- result = idx .delete (n )
241
- tm .assert_index_equal (result , expected )
242
- assert result .name == expected .name
243
- assert result .freq == expected .freq
244
-
245
- result = idx .delete (slice (n [0 ], n [- 1 ] + 1 ))
246
- tm .assert_index_equal (result , expected )
247
- assert result .name == expected .name
248
- assert result .freq == expected .freq
249
-
250
- def test_get_loc (self ):
251
- idx = pd .to_timedelta (["0 days" , "1 days" , "2 days" ])
252
-
253
- for method in [None , "pad" , "backfill" , "nearest" ]:
254
- assert idx .get_loc (idx [1 ], method ) == 1
255
- assert idx .get_loc (idx [1 ].to_pytimedelta (), method ) == 1
256
- assert idx .get_loc (str (idx [1 ]), method ) == 1
257
-
258
- assert idx .get_loc (idx [1 ], "pad" , tolerance = Timedelta (0 )) == 1
259
- assert idx .get_loc (idx [1 ], "pad" , tolerance = np .timedelta64 (0 , "s" )) == 1
260
- assert idx .get_loc (idx [1 ], "pad" , tolerance = timedelta (0 )) == 1
261
-
262
- with pytest .raises (ValueError , match = "unit abbreviation w/o a number" ):
263
- idx .get_loc (idx [1 ], method = "nearest" , tolerance = "foo" )
264
-
265
- with pytest .raises (ValueError , match = "tolerance size must match" ):
266
- idx .get_loc (
267
- idx [1 ],
268
- method = "nearest" ,
269
- tolerance = [
270
- Timedelta (0 ).to_timedelta64 (),
271
- Timedelta (0 ).to_timedelta64 (),
272
- ],
273
- )
274
-
275
- for method , loc in [("pad" , 1 ), ("backfill" , 2 ), ("nearest" , 1 )]:
276
- assert idx .get_loc ("1 day 1 hour" , method ) == loc
277
-
278
- # GH 16909
279
- assert idx .get_loc (idx [1 ].to_timedelta64 ()) == 1
280
-
281
- # GH 16896
282
- assert idx .get_loc ("0 days" ) == 0
283
-
284
- def test_get_loc_nat (self ):
285
- tidx = TimedeltaIndex (["1 days 01:00:00" , "NaT" , "2 days 01:00:00" ])
286
-
287
- assert tidx .get_loc (pd .NaT ) == 1
288
- assert tidx .get_loc (None ) == 1
289
- assert tidx .get_loc (float ("nan" )) == 1
290
- assert tidx .get_loc (np .nan ) == 1
291
-
292
- def test_get_indexer (self ):
293
- idx = pd .to_timedelta (["0 days" , "1 days" , "2 days" ])
294
- tm .assert_numpy_array_equal (
295
- idx .get_indexer (idx ), np .array ([0 , 1 , 2 ], dtype = np .intp )
296
- )
297
-
298
- target = pd .to_timedelta (["-1 hour" , "12 hours" , "1 day 1 hour" ])
299
- tm .assert_numpy_array_equal (
300
- idx .get_indexer (target , "pad" ), np .array ([- 1 , 0 , 1 ], dtype = np .intp )
301
- )
302
- tm .assert_numpy_array_equal (
303
- idx .get_indexer (target , "backfill" ), np .array ([0 , 1 , 2 ], dtype = np .intp )
304
- )
305
- tm .assert_numpy_array_equal (
306
- idx .get_indexer (target , "nearest" ), np .array ([0 , 1 , 1 ], dtype = np .intp )
307
- )
308
-
309
- res = idx .get_indexer (target , "nearest" , tolerance = Timedelta ("1 hour" ))
310
- tm .assert_numpy_array_equal (res , np .array ([0 , - 1 , 1 ], dtype = np .intp ))
0 commit comments