@@ -642,36 +642,34 @@ def get_loc(self, key, method=None, tolerance=None):
642
642
-------
643
643
loc : int
644
644
"""
645
- try :
646
- return self ._engine .get_loc (key )
647
- except KeyError :
648
- if is_integer (key ):
649
- raise
650
-
645
+ if isinstance (key , str ):
651
646
try :
652
- asdt , parsed , reso = parse_time_string (key , self .freq )
653
- key = asdt
654
- except TypeError :
655
- pass
647
+ key , parsed , reso = parse_time_string (key , self .freq )
656
648
except DateParseError :
657
649
# A string with invalid format
658
650
raise KeyError (f"Cannot interpret '{ key } ' as period" )
659
651
660
- try :
661
- key = Period (key , freq = self .freq )
662
- except ValueError :
663
- # we cannot construct the Period
664
- # as we have an invalid type
665
- raise KeyError (key )
652
+ elif is_integer (key ):
653
+ # Period constructor will cast to string, which we dont want
654
+ raise KeyError (key )
666
655
667
- try :
668
- ordinal = iNaT if key is NaT else key .ordinal
669
- if tolerance is not None :
670
- tolerance = self ._convert_tolerance (tolerance , np .asarray (key ))
671
- return self ._int64index .get_loc (ordinal , method , tolerance )
656
+ try :
657
+ key = Period (key , freq = self .freq )
658
+ ordinal = key .ordinal if key is not NaT else key .value
659
+ except ValueError :
660
+ # we cannot construct the Period
661
+ # as we have an invalid type
662
+ if is_list_like (key ):
663
+ raise TypeError (f"'{ key } ' is an invalid key" )
664
+ raise KeyError (key )
665
+
666
+ if tolerance is not None :
667
+ tolerance = self ._convert_tolerance (tolerance , np .asarray (key ))
672
668
673
- except KeyError :
674
- raise KeyError (key )
669
+ try :
670
+ return self ._int64index .get_loc (ordinal , method , tolerance )
671
+ except KeyError :
672
+ raise KeyError (key )
675
673
676
674
def _maybe_cast_slice_bound (self , label , side , kind ):
677
675
"""
0 commit comments