@@ -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
+ key , parsed , reso = parse_time_string (key , self .freq )
713
+ except DateParseError :
714
+ # A string with invalid format
715
+ raise KeyError (f"Cannot interpret '{ key } ' as period" )
721
716
722
717
try :
723
718
key = Period (key , freq = self .freq )
719
+ ordinal = iNaT if key is NaT else key .ordinal
724
720
except ValueError :
725
- # we cannot construct the Period
726
- # as we have an invalid type
727
- raise KeyError (key )
721
+ # we cannot construct the Period as we have an invalid type
722
+ raise TypeError (f"'{ key } ' is an invalid key" )
728
723
729
- try :
730
- 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 )
724
+ if tolerance is not None :
725
+ tolerance = self ._convert_tolerance (tolerance , np .asarray (key ))
734
726
735
- except KeyError :
736
- raise KeyError (key )
727
+ try :
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