@@ -2633,16 +2633,25 @@ def _should_fallback_to_positional(self) -> bool:
2633
2633
# GH#33355
2634
2634
return self .levels [0 ]._should_fallback_to_positional
2635
2635
2636
- def get_loc_result_index (self , loc , key ):
2636
+ def _get_values_for_loc (self , series : Series , loc , key ):
2637
2637
"""
2638
- Find the index to attach to the result of a Series.loc lookup.
2638
+ Do a positional lookup on the given Series, returning either a scalar
2639
+ or a Series.
2639
2640
2640
- Assumes `loc` is not an integer.
2641
-
2642
- key is included for MultiIndex compat.
2641
+ Assumes that `series.index is self`
2643
2642
"""
2644
- new_index = super ().get_loc_result_index (loc , key )
2645
- return maybe_droplevels (new_index , key )
2643
+ new_values = series ._values [loc ]
2644
+ if is_scalar (loc ):
2645
+ return new_values
2646
+
2647
+ if len (new_values ) == 1 and not self .nlevels > 1 :
2648
+ # If more than one level left, we can not return a scalar
2649
+ return new_values [0 ]
2650
+
2651
+ new_index = self [loc ]
2652
+ new_index = maybe_droplevels (new_index , key )
2653
+ new_ser = series ._constructor (new_values , index = new_index , name = series .name )
2654
+ return new_ser .__finalize__ (series )
2646
2655
2647
2656
def _get_indexer_strict (
2648
2657
self , key , axis_name : str
@@ -2949,15 +2958,6 @@ def _maybe_to_slice(loc):
2949
2958
2950
2959
if not isinstance (key , tuple ):
2951
2960
loc = self ._get_level_indexer (key , level = 0 )
2952
- if (
2953
- self .nlevels == 1
2954
- and isinstance (loc , slice )
2955
- and loc .stop - loc .start == 1
2956
- and loc .step is None
2957
- ):
2958
- # e.g. test_multiindex_at_get_one_level
2959
- # TODO: is this the right level at which to do this check?
2960
- return loc .start
2961
2961
return _maybe_to_slice (loc )
2962
2962
2963
2963
keylen = len (key )
0 commit comments