Skip to content

Commit 92bb4c9

Browse files
authored
REF: implement unpack_1tuple to clean up Series.__getitem__ (#31906)
1 parent c81b0ba commit 92bb4c9

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

pandas/core/indexers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,33 @@ def deprecate_ndim_indexing(result):
270270
)
271271

272272

273+
def unpack_1tuple(tup):
274+
"""
275+
If we have a length-1 tuple/list that contains a slice, unpack to just
276+
the slice.
277+
278+
Notes
279+
-----
280+
The list case is deprecated.
281+
"""
282+
if len(tup) == 1 and isinstance(tup[0], slice):
283+
# if we don't have a MultiIndex, we may still be able to handle
284+
# a 1-tuple. see test_1tuple_without_multiindex
285+
286+
if isinstance(tup, list):
287+
# GH#31299
288+
warnings.warn(
289+
"Indexing with a single-item list containing a "
290+
"slice is deprecated and will raise in a future "
291+
"version. Pass a tuple instead.",
292+
FutureWarning,
293+
stacklevel=3,
294+
)
295+
296+
return tup[0]
297+
return tup
298+
299+
273300
# -----------------------------------------------------------
274301
# Public indexer validation
275302

pandas/core/series.py

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
sanitize_array,
7373
)
7474
from pandas.core.generic import NDFrame
75-
from pandas.core.indexers import maybe_convert_indices
75+
from pandas.core.indexers import maybe_convert_indices, unpack_1tuple
7676
from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
7777
from pandas.core.indexes.api import (
7878
Float64Index,
@@ -851,6 +851,8 @@ def __getitem__(self, key):
851851
key_is_scalar = is_scalar(key)
852852
if key_is_scalar:
853853
key = self.index._convert_scalar_indexer(key, kind="getitem")
854+
elif isinstance(key, (list, tuple)):
855+
key = unpack_1tuple(key)
854856

855857
if key_is_scalar or isinstance(self.index, MultiIndex):
856858
# Otherwise index.get_value will raise InvalidIndexError
@@ -893,16 +895,7 @@ def _get_with(self, key):
893895
"supported, use the appropriate DataFrame column"
894896
)
895897
elif isinstance(key, tuple):
896-
try:
897-
return self._get_values_tuple(key)
898-
except ValueError:
899-
# if we don't have a MultiIndex, we may still be able to handle
900-
# a 1-tuple. see test_1tuple_without_multiindex
901-
if len(key) == 1:
902-
key = key[0]
903-
if isinstance(key, slice):
904-
return self._get_values(key)
905-
raise
898+
return self._get_values_tuple(key)
906899

907900
if not isinstance(key, (list, np.ndarray, ExtensionArray, Series, Index)):
908901
key = list(key)
@@ -924,26 +917,8 @@ def _get_with(self, key):
924917
else:
925918
return self.iloc[key]
926919

927-
if isinstance(key, (list, tuple)):
928-
# TODO: de-dup with tuple case handled above?
920+
if isinstance(key, list):
929921
# handle the dup indexing case GH#4246
930-
if len(key) == 1 and isinstance(key[0], slice):
931-
# [slice(0, 5, None)] will break if you convert to ndarray,
932-
# e.g. as requested by np.median
933-
# FIXME: hack
934-
if isinstance(key, list):
935-
# GH#31299
936-
warnings.warn(
937-
"Indexing with a single-item list containing a "
938-
"slice is deprecated and will raise in a future "
939-
"version. Pass a tuple instead.",
940-
FutureWarning,
941-
stacklevel=3,
942-
)
943-
# TODO: use a message more like numpy's?
944-
key = tuple(key)
945-
return self._get_values(key)
946-
947922
return self.loc[key]
948923

949924
return self.reindex(key)

0 commit comments

Comments
 (0)