Skip to content

REF: implement unpack_1tuple to clean up Series.__getitem__ #31906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,33 @@ def deprecate_ndim_indexing(result):
)


def unpack_1tuple(tup):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type when you can Union[List, Tuple] ?

If we have a length-1 tuple/list that contains a slice, unpack to just
the slice.

Notes
-----
The list case is deprecated.
"""
if len(tup) == 1 and isinstance(tup[0], slice):
# if we don't have a MultiIndex, we may still be able to handle
# a 1-tuple. see test_1tuple_without_multiindex

if isinstance(tup, list):
# GH#31299
warnings.warn(
"Indexing with a single-item list containing a "
"slice is deprecated and will raise in a future "
"version. Pass a tuple instead.",
FutureWarning,
stacklevel=3,
)

return tup[0]
return tup


# -----------------------------------------------------------
# Public indexer validation

Expand Down
35 changes: 5 additions & 30 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
sanitize_array,
)
from pandas.core.generic import NDFrame
from pandas.core.indexers import maybe_convert_indices
from pandas.core.indexers import maybe_convert_indices, unpack_1tuple
from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
from pandas.core.indexes.api import (
Float64Index,
Expand Down Expand Up @@ -851,6 +851,8 @@ def __getitem__(self, key):
key_is_scalar = is_scalar(key)
if key_is_scalar:
key = self.index._convert_scalar_indexer(key, kind="getitem")
elif isinstance(key, (list, tuple)):
key = unpack_1tuple(key)

if key_is_scalar or isinstance(self.index, MultiIndex):
# Otherwise index.get_value will raise InvalidIndexError
Expand Down Expand Up @@ -893,16 +895,7 @@ def _get_with(self, key):
"supported, use the appropriate DataFrame column"
)
elif isinstance(key, tuple):
try:
return self._get_values_tuple(key)
except ValueError:
# if we don't have a MultiIndex, we may still be able to handle
# a 1-tuple. see test_1tuple_without_multiindex
if len(key) == 1:
key = key[0]
if isinstance(key, slice):
return self._get_values(key)
raise
return self._get_values_tuple(key)

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

if isinstance(key, (list, tuple)):
# TODO: de-dup with tuple case handled above?
if isinstance(key, list):
# handle the dup indexing case GH#4246
if len(key) == 1 and isinstance(key[0], slice):
# [slice(0, 5, None)] will break if you convert to ndarray,
# e.g. as requested by np.median
# FIXME: hack
if isinstance(key, list):
# GH#31299
warnings.warn(
"Indexing with a single-item list containing a "
"slice is deprecated and will raise in a future "
"version. Pass a tuple instead.",
FutureWarning,
stacklevel=3,
)
# TODO: use a message more like numpy's?
key = tuple(key)
return self._get_values(key)

return self.loc[key]

return self.reindex(key)
Expand Down