Skip to content

Commit 0af361b

Browse files
committed
try again
1 parent 1ee9ad9 commit 0af361b

File tree

3 files changed

+37
-44
lines changed

3 files changed

+37
-44
lines changed

pandas/core/indexes/base.py

+4-21
Original file line numberDiff line numberDiff line change
@@ -6018,9 +6018,6 @@ def get_value(self, series: Series, key):
60186018
else:
60196019
raise
60206020

6021-
if is_integer(loc):
6022-
return series._values[loc]
6023-
60246021
return self._get_values_for_loc(series, loc, key)
60256022

60266023
def _check_indexing_error(self, key):
@@ -6036,33 +6033,19 @@ def _should_fallback_to_positional(self) -> bool:
60366033
"""
60376034
return not self.holds_integer()
60386035

6039-
def get_loc_result_index(self, loc, key):
6040-
"""
6041-
Find the index to attach to the result of a Series.loc lookup.
6042-
6043-
Assumes `loc` is not an integer.
6044-
6045-
key is included for MultiIndex compat.
6046-
"""
6047-
return self[loc]
6048-
6049-
# TODO(2.0): remove once get_value deprecation is enforced
6050-
@final
60516036
def _get_values_for_loc(self, series: Series, loc, key):
60526037
"""
60536038
Do a positional lookup on the given Series, returning either a scalar
60546039
or a Series.
60556040
6056-
Assumes that `series.index is self` and that `loc` is not an integer,
6057-
the result of self.get_loc(key)
6041+
Assumes that `series.index is self`
60586042
60596043
key is included for MultiIndex compat.
60606044
"""
6061-
new_values = series._values[loc]
6045+
if is_integer(loc):
6046+
return series._values[loc]
60626047

6063-
new_index = self.get_loc_result_index(loc, key)
6064-
new_ser = series._constructor(new_values, index=new_index, name=series.name)
6065-
return new_ser.__finalize__(series)
6048+
return series.iloc[loc]
60666049

60676050
@final
60686051
def set_value(self, arr, key, value) -> None:

pandas/core/indexes/multi.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -2633,16 +2633,25 @@ def _should_fallback_to_positional(self) -> bool:
26332633
# GH#33355
26342634
return self.levels[0]._should_fallback_to_positional
26352635

2636-
def get_loc_result_index(self, loc, key):
2636+
def _get_values_for_loc(self, series: Series, loc, key):
26372637
"""
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.
26392640
2640-
Assumes `loc` is not an integer.
2641-
2642-
key is included for MultiIndex compat.
2641+
Assumes that `series.index is self`
26432642
"""
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)
26462655

26472656
def _get_indexer_strict(
26482657
self, key, axis_name: str
@@ -2949,15 +2958,6 @@ def _maybe_to_slice(loc):
29492958

29502959
if not isinstance(key, tuple):
29512960
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
29612961
return _maybe_to_slice(loc)
29622962

29632963
keylen = len(key)

pandas/core/series.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
ensure_index,
141141
)
142142
import pandas.core.indexes.base as ibase
143+
from pandas.core.indexes.multi import maybe_droplevels
143144
from pandas.core.indexing import (
144145
check_bool_indexer,
145146
check_deprecated_indexers,
@@ -1060,17 +1061,26 @@ def _get_value(self, label, takeable: bool = False):
10601061
if takeable:
10611062
return self._values[label]
10621063

1063-
index = self.index
1064-
10651064
# Similar to Index.get_value, but we do not fall back to positional
10661065
loc = self.index.get_loc(label)
1067-
res_values = self._values[loc]
1066+
10681067
if is_integer(loc):
1069-
return res_values
1068+
return self._values[loc]
10701069

1071-
res_index = index.get_loc_result_index(loc, label)
1072-
result = self._constructor(res_values, index=res_index, name=self.name)
1073-
return result.__finalize__(self)
1070+
if isinstance(self.index, MultiIndex):
1071+
mi = self.index
1072+
new_values = self._values[loc]
1073+
if len(new_values) == 1 and mi.nlevels == 1:
1074+
# If more than one level left, we can not return a scalar
1075+
return new_values[0]
1076+
1077+
new_index = mi[loc]
1078+
new_index = maybe_droplevels(new_index, label)
1079+
new_ser = self._constructor(new_values, index=new_index, name=self.name)
1080+
return new_ser.__finalize__(self)
1081+
1082+
else:
1083+
return self.iloc[loc]
10741084

10751085
def __setitem__(self, key, value) -> None:
10761086
check_deprecated_indexers(key)

0 commit comments

Comments
 (0)