diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 920919755dc23..d47e434c21b1f 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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`) - - diff --git a/pandas/core/series.py b/pandas/core/series.py index e5cea8ebfc914..56675dbd8b685 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 " + "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] diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index 77085ef547690..8364e2d2bfe65 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -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) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 18dbd22b73b35..bd3c0da51e180 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -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)