From a841ab646cce56b2ebda208f3cc8f9573dedad7a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:34:24 -0700 Subject: [PATCH] DEPR: Enforce diallowing indexing with single item slice --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/indexers/utils.py | 7 ++----- pandas/tests/series/indexing/test_getitem.py | 9 ++++----- pandas/tests/series/indexing/test_indexing.py | 9 ++++----- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 78ea78ec97a3a..252c444b2e60c 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -248,6 +248,7 @@ Removal of prior version deprecations/changes - Removed setting Categorical._codes directly (:issue:`41429`) - Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`) - Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`) +- Enforced disallowing indexing a :class:`Series` with a single item list with a slice (e.g. ``ser[[slice(0, 2)]]``). Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - Enforced the ``display.max_colwidth`` option to not accept negative integers (:issue:`31569`) - Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`) - Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`) diff --git a/pandas/core/indexers/utils.py b/pandas/core/indexers/utils.py index 0f3cdc4195c85..50b53ecb63082 100644 --- a/pandas/core/indexers/utils.py +++ b/pandas/core/indexers/utils.py @@ -367,12 +367,9 @@ def unpack_1tuple(tup): if isinstance(tup, list): # GH#31299 - warnings.warn( + raise ValueError( "Indexing with a single-item list containing a " - "slice is deprecated and will raise in a future " - "version. Pass a tuple instead.", - FutureWarning, - stacklevel=find_stack_level(), + "slice is not allowed. Pass a tuple instead.", ) return tup[0] diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 993c056045ae0..e3a38fcd10642 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -284,14 +284,13 @@ def test_getitem_slice_2d(self, datetime_series): @pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning") def test_getitem_median_slice_bug(self): index = date_range("20090415", "20090519", freq="2B") - s = Series(np.random.randn(13), index=index) + ser = Series(np.random.randn(13), index=index) indexer = [slice(6, 7, None)] - with tm.assert_produces_warning(FutureWarning): + msg = "Indexing with a single-item list" + with pytest.raises(ValueError, match=msg): # GH#31299 - result = s[indexer] - expected = s[indexer[0]] - tm.assert_series_equal(result, expected) + ser[indexer] @pytest.mark.parametrize( "slc, positions", diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index e3df9671e6c64..7e4912ec88d36 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -194,12 +194,11 @@ def test_basic_getitem_setitem_corner(datetime_series): with pytest.raises(KeyError, match=msg): datetime_series[:, 2] = 2 - # weird lists. [slice(0, 5)] will work but not two slices - with tm.assert_produces_warning(FutureWarning): + # weird lists. [slice(0, 5)] raises but not two slices + msg = "Indexing with a single-item list" + with pytest.raises(ValueError, match=msg): # GH#31299 - result = datetime_series[[slice(None, 5)]] - expected = datetime_series[:5] - tm.assert_series_equal(result, expected) + datetime_series[[slice(None, 5)]] # OK msg = r"unhashable type(: 'slice')?"