Skip to content

DEPR: Enforce diallowing indexing with single item slice #49343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/indexers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')?"
Expand Down