From 78be1461754d438b8f2c9777c3aba7ea8454a2d6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 26 Jan 2020 20:20:02 -0800 Subject: [PATCH 1/3] DEPR: indexing Series with single-entry list --- pandas/core/series.py | 11 +++++++++++ pandas/tests/series/indexing/test_datetime.py | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index e79404ccd9521..e228f14988e66 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -917,6 +917,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) From eccd2116d9fae707d04e56be7e0984839674b3bd Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 27 Jan 2020 11:53:43 -0800 Subject: [PATCH 2/3] catch FutureWarning --- pandas/tests/series/indexing/test_indexing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 65731cf45bd2d..23ec4573f5975 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) From a761a2ae9a38c0aa225a70a3dd1dd23cc95ed043 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 30 Jan 2020 17:17:50 -0800 Subject: [PATCH 3/3] whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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`) - -