@@ -526,29 +526,35 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: dt.datetime):
526526 "The index must be timezone aware when indexing "
527527 "with a date string with a UTC offset"
528528 )
529- start = self ._maybe_cast_for_get_loc (start )
530- end = self ._maybe_cast_for_get_loc (end )
529+ # The flipped case with parsed.tz is None and self.tz is not None
530+ # is ruled out bc parsed and reso are produced by _parse_with_reso,
531+ # which localizes parsed.
531532 return start , end
532533
533- def _disallow_mismatched_indexing (self , key , one_way : bool = False ) -> None :
534+ def _parse_with_reso (self , label : str ):
535+ parsed , reso = super ()._parse_with_reso (label )
536+
537+ parsed = Timestamp (parsed )
538+
539+ if self .tz is not None and parsed .tzinfo is None :
540+ # we special-case timezone-naive strings and timezone-aware
541+ # DatetimeIndex
542+ # https://github.com/pandas-dev/pandas/pull/36148#issuecomment-687883081
543+ parsed = parsed .tz_localize (self .tz )
544+
545+ return parsed , reso
546+
547+ def _disallow_mismatched_indexing (self , key ) -> None :
534548 """
535549 Check for mismatched-tzawareness indexing and re-raise as KeyError.
536550 """
551+ # we get here with isinstance(key, self._data._recognized_scalars)
537552 try :
538- self ._deprecate_mismatched_indexing (key , one_way = one_way )
553+ # GH#36148
554+ self ._data ._assert_tzawareness_compat (key )
539555 except TypeError as err :
540556 raise KeyError (key ) from err
541557
542- def _deprecate_mismatched_indexing (self , key , one_way : bool = False ) -> None :
543- # GH#36148
544- # we get here with isinstance(key, self._data._recognized_scalars)
545- if self .tz is not None and one_way :
546- # we special-case timezone-naive strings and timezone-aware
547- # DatetimeIndex
548- return
549-
550- self ._data ._assert_tzawareness_compat (key )
551-
552558 def get_loc (self , key , method = None , tolerance = None ):
553559 """
554560 Get integer location for requested label
@@ -566,15 +572,15 @@ def get_loc(self, key, method=None, tolerance=None):
566572 if isinstance (key , self ._data ._recognized_scalars ):
567573 # needed to localize naive datetimes
568574 self ._disallow_mismatched_indexing (key )
569- key = self . _maybe_cast_for_get_loc (key )
575+ key = Timestamp (key )
570576
571577 elif isinstance (key , str ):
572578
573579 try :
574580 parsed , reso = self ._parse_with_reso (key )
575581 except ValueError as err :
576582 raise KeyError (key ) from err
577- self ._disallow_mismatched_indexing (parsed , one_way = True )
583+ self ._disallow_mismatched_indexing (parsed )
578584
579585 if self ._can_partial_date_slice (reso ):
580586 try :
@@ -583,7 +589,7 @@ def get_loc(self, key, method=None, tolerance=None):
583589 if method is None :
584590 raise KeyError (key ) from err
585591
586- key = self . _maybe_cast_for_get_loc ( key )
592+ key = parsed
587593
588594 elif isinstance (key , dt .timedelta ):
589595 # GH#20464
@@ -607,24 +613,6 @@ def get_loc(self, key, method=None, tolerance=None):
607613 except KeyError as err :
608614 raise KeyError (orig_key ) from err
609615
610- def _maybe_cast_for_get_loc (self , key ) -> Timestamp :
611- # needed to localize naive datetimes or dates (GH 35690)
612- try :
613- key = Timestamp (key )
614- except ValueError as err :
615- # FIXME(dateutil#1180): we get here because parse_with_reso
616- # doesn't raise on "t2m"
617- if not isinstance (key , str ):
618- # Not expected to be reached, but check to be sure
619- raise # pragma: no cover
620- raise KeyError (key ) from err
621-
622- if key .tzinfo is None :
623- key = key .tz_localize (self .tz )
624- else :
625- key = key .tz_convert (self .tz )
626- return key
627-
628616 @doc (DatetimeTimedeltaMixin ._maybe_cast_slice_bound )
629617 def _maybe_cast_slice_bound (self , label , side : str ):
630618
@@ -635,8 +623,8 @@ def _maybe_cast_slice_bound(self, label, side: str):
635623 label = Timestamp (label ).to_pydatetime ()
636624
637625 label = super ()._maybe_cast_slice_bound (label , side )
638- self ._deprecate_mismatched_indexing (label )
639- return self . _maybe_cast_for_get_loc (label )
626+ self ._data . _assert_tzawareness_compat (label )
627+ return Timestamp (label )
640628
641629 def slice_indexer (self , start = None , end = None , step = None ):
642630 """
0 commit comments