Skip to content

Commit 1321905

Browse files
committed
Removed duplicate code by moving changes to _AtIndexer._convert_key
- More natural place for key modification to occur - __getitem__ and __setitem__ now remain essentially unchanged
1 parent a2f4bd1 commit 1321905

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

pandas/core/indexing.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,10 +2031,6 @@ def __getitem__(self, key):
20312031
raise ValueError("Invalid call for scalar access (getting)!")
20322032

20332033
key = self._convert_key(key)
2034-
2035-
if isinstance(self.obj, ABCSeries) and len(key) > 1:
2036-
key = [key]
2037-
20382034
return self.obj._get_value(*key, takeable=self._takeable)
20392035

20402036
def __setitem__(self, key, value):
@@ -2046,13 +2042,9 @@ def __setitem__(self, key, value):
20462042

20472043
if not isinstance(key, tuple):
20482044
key = _tuplify(self.ndim, key)
2049-
2050-
if isinstance(self.obj, ABCSeries) and len(key) > 1:
2051-
key = [key]
2052-
2045+
key = list(self._convert_key(key, is_setter=True))
20532046
if len(key) != self.ndim:
20542047
raise ValueError("Not enough indexers for scalar access (setting)!")
2055-
key = list(self._convert_key(key, is_setter=True))
20562048
self.obj._set_value(*key, value=value, takeable=self._takeable)
20572049

20582050

@@ -2065,6 +2057,11 @@ def _convert_key(self, key, is_setter: bool = False):
20652057
Require they keys to be the same type as the index. (so we don't
20662058
fallback)
20672059
"""
2060+
# For series, unpacking key needs to result in the label.
2061+
# This is already the case for non-multiindex
2062+
if isinstance(self.obj, ABCSeries) and len(key) > 1:
2063+
key = (key,)
2064+
20682065
# allow arbitrary setting
20692066
if is_setter:
20702067
return list(key)

0 commit comments

Comments
 (0)