1
1
""" implement the TimedeltaIndex """
2
- from datetime import datetime
3
2
4
3
import numpy as np
5
4
10
9
_TD_DTYPE ,
11
10
is_float ,
12
11
is_integer ,
13
- is_list_like ,
14
12
is_scalar ,
15
13
is_timedelta64_dtype ,
16
14
is_timedelta64_ns_dtype ,
17
15
pandas_dtype ,
18
16
)
19
- from pandas .core .dtypes .missing import isna
17
+ from pandas .core .dtypes .missing import is_valid_nat_for_dtype
20
18
21
19
from pandas .core .accessor import delegate_names
22
20
from pandas .core .arrays import datetimelike as dtl
23
- from pandas .core .arrays .timedeltas import TimedeltaArray , _is_convertible_to_td
21
+ from pandas .core .arrays .timedeltas import TimedeltaArray
24
22
import pandas .core .common as com
25
- from pandas .core .indexes .base import Index , _index_shared_docs , maybe_extract_name
23
+ from pandas .core .indexes .base import (
24
+ Index ,
25
+ InvalidIndexError ,
26
+ _index_shared_docs ,
27
+ maybe_extract_name ,
28
+ )
26
29
from pandas .core .indexes .datetimelike import (
27
30
DatetimeIndexOpsMixin ,
28
31
DatetimelikeDelegateMixin ,
@@ -236,22 +239,10 @@ def get_value(self, series, key):
236
239
Fast lookup of value from 1-dimensional ndarray. Only use this if you
237
240
know what you're doing
238
241
"""
239
-
240
- if isinstance (key , str ):
241
- try :
242
- key = Timedelta (key )
243
- except ValueError :
244
- raise KeyError (key )
245
-
246
- if isinstance (key , self ._data ._recognized_scalars ) or key is NaT :
247
- key = Timedelta (key )
248
- return self .get_value_maybe_box (series , key )
249
-
250
- value = Index .get_value (self , series , key )
251
- return com .maybe_box (self , value , series , key )
252
-
253
- def get_value_maybe_box (self , series , key : Timedelta ):
254
- loc = self .get_loc (key )
242
+ if is_integer (key ):
243
+ loc = key
244
+ else :
245
+ loc = self .get_loc (key )
255
246
return self ._get_values_for_loc (series , loc )
256
247
257
248
def get_loc (self , key , method = None , tolerance = None ):
@@ -260,27 +251,31 @@ def get_loc(self, key, method=None, tolerance=None):
260
251
261
252
Returns
262
253
-------
263
- loc : int
254
+ loc : int, slice, or ndarray[int]
264
255
"""
265
- if is_list_like (key ) or (isinstance (key , datetime ) and key is not NaT ):
266
- # GH#20464 datetime check here is to ensure we don't allow
267
- # datetime objects to be incorrectly treated as timedelta
268
- # objects; NaT is a special case because it plays a double role
269
- # as Not-A-Timedelta
270
- raise TypeError
271
-
272
- if isna (key ):
256
+ if not is_scalar (key ):
257
+ raise InvalidIndexError (key )
258
+
259
+ if is_valid_nat_for_dtype (key , self .dtype ):
273
260
key = NaT
274
261
262
+ elif isinstance (key , str ):
263
+ try :
264
+ key = Timedelta (key )
265
+ except ValueError :
266
+ raise KeyError (key )
267
+
268
+ elif isinstance (key , self ._data ._recognized_scalars ) or key is NaT :
269
+ key = Timedelta (key )
270
+
271
+ else :
272
+ raise KeyError (key )
273
+
275
274
if tolerance is not None :
276
275
# try converting tolerance now, so errors don't get swallowed by
277
276
# the try/except clauses below
278
277
tolerance = self ._convert_tolerance (tolerance , np .asarray (key ))
279
278
280
- if _is_convertible_to_td (key ) or key is NaT :
281
- key = Timedelta (key )
282
- return Index .get_loc (self , key , method , tolerance )
283
-
284
279
return Index .get_loc (self , key , method , tolerance )
285
280
286
281
def _maybe_cast_slice_bound (self , label , side , kind ):
0 commit comments