Skip to content

Commit 04e9a78

Browse files
authored
DEPR: indexing Series with single-entry list (#31333)
1 parent 0a52507 commit 04e9a78

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Backwards incompatible API changes
7272

7373
Deprecations
7474
~~~~~~~~~~~~
75-
75+
- 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`)
7676
-
7777
-
7878

pandas/core/series.py

+11
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,17 @@ def _get_with(self, key):
931931
# [slice(0, 5, None)] will break if you convert to ndarray,
932932
# e.g. as requested by np.median
933933
# 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)
934945
return self._get_values(key)
935946

936947
return self.loc[key]

pandas/tests/series/indexing/test_datetime.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,9 @@ def test_getitem_median_slice_bug():
364364
s = Series(np.random.randn(13), index=index)
365365

366366
indexer = [slice(6, 7, None)]
367-
result = s[indexer]
367+
with tm.assert_produces_warning(FutureWarning):
368+
# GH#31299
369+
result = s[indexer]
368370
expected = s[indexer[0]]
369371
tm.assert_series_equal(result, expected)
370372

pandas/tests/series/indexing/test_indexing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ def test_basic_getitem_setitem_corner(datetime_series):
424424
datetime_series[:, 2] = 2
425425

426426
# weird lists. [slice(0, 5)] will work but not two slices
427-
result = datetime_series[[slice(None, 5)]]
427+
with tm.assert_produces_warning(FutureWarning):
428+
# GH#31299
429+
result = datetime_series[[slice(None, 5)]]
428430
expected = datetime_series[:5]
429431
tm.assert_series_equal(result, expected)
430432

0 commit comments

Comments
 (0)