Skip to content

Commit 5b51e56

Browse files
mroeschkenoatamir
authored andcommitted
DEPR: Enforce diallowing indexing with single item slice (pandas-dev#49343)
1 parent c448512 commit 5b51e56

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Removal of prior version deprecations/changes
248248
- Removed setting Categorical._codes directly (:issue:`41429`)
249249
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
250250
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)
251+
- 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`)
251252
- Enforced the ``display.max_colwidth`` option to not accept negative integers (:issue:`31569`)
252253
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
253254
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)

pandas/core/indexers/utils.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,9 @@ def unpack_1tuple(tup):
367367

368368
if isinstance(tup, list):
369369
# GH#31299
370-
warnings.warn(
370+
raise ValueError(
371371
"Indexing with a single-item list containing a "
372-
"slice is deprecated and will raise in a future "
373-
"version. Pass a tuple instead.",
374-
FutureWarning,
375-
stacklevel=find_stack_level(),
372+
"slice is not allowed. Pass a tuple instead.",
376373
)
377374

378375
return tup[0]

pandas/tests/series/indexing/test_getitem.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,13 @@ def test_getitem_slice_2d(self, datetime_series):
284284
@pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning")
285285
def test_getitem_median_slice_bug(self):
286286
index = date_range("20090415", "20090519", freq="2B")
287-
s = Series(np.random.randn(13), index=index)
287+
ser = Series(np.random.randn(13), index=index)
288288

289289
indexer = [slice(6, 7, None)]
290-
with tm.assert_produces_warning(FutureWarning):
290+
msg = "Indexing with a single-item list"
291+
with pytest.raises(ValueError, match=msg):
291292
# GH#31299
292-
result = s[indexer]
293-
expected = s[indexer[0]]
294-
tm.assert_series_equal(result, expected)
293+
ser[indexer]
295294

296295
@pytest.mark.parametrize(
297296
"slc, positions",

pandas/tests/series/indexing/test_indexing.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,11 @@ def test_basic_getitem_setitem_corner(datetime_series):
194194
with pytest.raises(KeyError, match=msg):
195195
datetime_series[:, 2] = 2
196196

197-
# weird lists. [slice(0, 5)] will work but not two slices
198-
with tm.assert_produces_warning(FutureWarning):
197+
# weird lists. [slice(0, 5)] raises but not two slices
198+
msg = "Indexing with a single-item list"
199+
with pytest.raises(ValueError, match=msg):
199200
# GH#31299
200-
result = datetime_series[[slice(None, 5)]]
201-
expected = datetime_series[:5]
202-
tm.assert_series_equal(result, expected)
201+
datetime_series[[slice(None, 5)]]
203202

204203
# OK
205204
msg = r"unhashable type(: 'slice')?"

0 commit comments

Comments
 (0)