Skip to content

DEPR: indexing Series with single-entry list #31333

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 6 commits into from
Feb 9, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Backwards incompatible API changes

Deprecations
~~~~~~~~~~~~

- Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`)
-
-

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,17 @@ def _get_with(self, key):
# [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 "
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we know what it will raise? Or will it depend on the value?

Copy link
Member Author

Choose a reason for hiding this comment

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

Best guess is TypeError, depends on where we let it fall through

"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]
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ def test_getitem_median_slice_bug():
s = Series(np.random.randn(13), index=index)

indexer = [slice(6, 7, None)]
result = s[indexer]
with tm.assert_produces_warning(FutureWarning):
# GH#31299
result = s[indexer]
expected = s[indexer[0]]
tm.assert_series_equal(result, expected)

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ def test_basic_getitem_setitem_corner(datetime_series):
datetime_series[:, 2] = 2

# weird lists. [slice(0, 5)] will work but not two slices
result = datetime_series[[slice(None, 5)]]
with tm.assert_produces_warning(FutureWarning):
# GH#31299
result = datetime_series[[slice(None, 5)]]
expected = datetime_series[:5]
tm.assert_series_equal(result, expected)

Expand Down