@@ -526,29 +526,35 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: dt.datetime):
526
526
"The index must be timezone aware when indexing "
527
527
"with a date string with a UTC offset"
528
528
)
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.
531
532
return start , end
532
533
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 :
534
548
"""
535
549
Check for mismatched-tzawareness indexing and re-raise as KeyError.
536
550
"""
551
+ # we get here with isinstance(key, self._data._recognized_scalars)
537
552
try :
538
- self ._deprecate_mismatched_indexing (key , one_way = one_way )
553
+ # GH#36148
554
+ self ._data ._assert_tzawareness_compat (key )
539
555
except TypeError as err :
540
556
raise KeyError (key ) from err
541
557
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
-
552
558
def get_loc (self , key , method = None , tolerance = None ):
553
559
"""
554
560
Get integer location for requested label
@@ -566,15 +572,15 @@ def get_loc(self, key, method=None, tolerance=None):
566
572
if isinstance (key , self ._data ._recognized_scalars ):
567
573
# needed to localize naive datetimes
568
574
self ._disallow_mismatched_indexing (key )
569
- key = self . _maybe_cast_for_get_loc (key )
575
+ key = Timestamp (key )
570
576
571
577
elif isinstance (key , str ):
572
578
573
579
try :
574
580
parsed , reso = self ._parse_with_reso (key )
575
581
except ValueError as err :
576
582
raise KeyError (key ) from err
577
- self ._disallow_mismatched_indexing (parsed , one_way = True )
583
+ self ._disallow_mismatched_indexing (parsed )
578
584
579
585
if self ._can_partial_date_slice (reso ):
580
586
try :
@@ -583,7 +589,7 @@ def get_loc(self, key, method=None, tolerance=None):
583
589
if method is None :
584
590
raise KeyError (key ) from err
585
591
586
- key = self . _maybe_cast_for_get_loc ( key )
592
+ key = parsed
587
593
588
594
elif isinstance (key , dt .timedelta ):
589
595
# GH#20464
@@ -607,24 +613,6 @@ def get_loc(self, key, method=None, tolerance=None):
607
613
except KeyError as err :
608
614
raise KeyError (orig_key ) from err
609
615
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
-
628
616
@doc (DatetimeTimedeltaMixin ._maybe_cast_slice_bound )
629
617
def _maybe_cast_slice_bound (self , label , side : str ):
630
618
@@ -635,8 +623,8 @@ def _maybe_cast_slice_bound(self, label, side: str):
635
623
label = Timestamp (label ).to_pydatetime ()
636
624
637
625
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 )
640
628
641
629
def slice_indexer (self , start = None , end = None , step = None ):
642
630
"""
0 commit comments