@@ -704,36 +704,30 @@ def get_loc(self, key, method=None, tolerance=None):
704
704
-------
705
705
loc : int
706
706
"""
707
- try :
708
- return self ._engine .get_loc (key )
709
- except KeyError :
710
- if is_integer (key ):
711
- raise
712
-
713
- try :
714
- asdt , parsed , reso = parse_time_string (key , self .freq )
715
- key = asdt
716
- except TypeError :
717
- pass
718
- except DateParseError :
719
- # A string with invalid format
720
- raise KeyError ("Cannot interpret '{}' as period" .format (key ))
707
+ if is_integer (key ) or (is_float (key ) and not isna (key )):
708
+ ordinal = key
709
+ else :
710
+ if isinstance (key , str ):
711
+ try :
712
+ asdt , parsed , reso = parse_time_string (key , self .freq )
713
+ key = asdt
714
+ except DateParseError :
715
+ # A string with invalid format
716
+ raise KeyError ("Cannot interpret '{}' as period" .format (key ))
721
717
722
718
try :
723
719
key = Period (key , freq = self .freq )
724
- except ValueError :
725
- # we cannot construct the Period
726
- # as we have an invalid type
727
- raise KeyError (key )
728
-
729
- try :
730
720
ordinal = iNaT if key is NaT else key .ordinal
731
- if tolerance is not None :
732
- tolerance = self . _convert_tolerance ( tolerance , np . asarray ( key ))
733
- return self . _int64index . get_loc ( ordinal , method , tolerance )
721
+ except ValueError :
722
+ # we cannot construct the Period as we have an invalid type
723
+ raise TypeError ( "'{val}' is an invalid key" . format ( val = key ) )
734
724
735
- except KeyError :
736
- raise KeyError (key )
725
+ try :
726
+ if tolerance is not None :
727
+ tolerance = self ._convert_tolerance (tolerance , np .asarray (key ))
728
+ return self ._int64index .get_loc (ordinal , method , tolerance )
729
+ except KeyError :
730
+ raise KeyError (key )
737
731
738
732
def _maybe_cast_slice_bound (self , label , side , kind ):
739
733
"""
0 commit comments