Skip to content

Commit 1613f26

Browse files
authored
REV: revert deprecation of Series.__getitem__ slicing with IntegerIndex (#50283)
1 parent 2f993c2 commit 1613f26

File tree

10 files changed

+15
-66
lines changed

10 files changed

+15
-66
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bug fixes
3737

3838
Other
3939
~~~~~
40+
- Reverted deprecation (:issue:`45324`) of behavior of :meth:`Series.__getitem__` and :meth:`Series.__setitem__` slicing with an integer :class:`Index`; this will remain positional (:issue:`49612`)
4041
-
4142

4243
.. ---------------------------------------------------------------------------

pandas/core/frame.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3648,9 +3648,7 @@ def __getitem__(self, key):
36483648
return self._getitem_multilevel(key)
36493649
# Do we have a slicer (on rows)?
36503650
if isinstance(key, slice):
3651-
indexer = self.index._convert_slice_indexer(
3652-
key, kind="getitem", is_frame=True
3653-
)
3651+
indexer = self.index._convert_slice_indexer(key, kind="getitem")
36543652
if isinstance(indexer, np.ndarray):
36553653
# reachable with DatetimeIndex
36563654
indexer = lib.maybe_indices_to_slice(
@@ -3829,7 +3827,7 @@ def __setitem__(self, key, value):
38293827

38303828
# see if we can slice the rows
38313829
if isinstance(key, slice):
3832-
slc = self.index._convert_slice_indexer(key, kind="getitem", is_frame=True)
3830+
slc = self.index._convert_slice_indexer(key, kind="getitem")
38333831
return self._setitem_slice(slc, value)
38343832

38353833
if isinstance(key, DataFrame) or getattr(key, "ndim", None) == 2:

pandas/core/indexes/base.py

+1-41
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
ABCDatetimeIndex,
124124
ABCMultiIndex,
125125
ABCPeriodIndex,
126-
ABCRangeIndex,
127126
ABCSeries,
128127
ABCTimedeltaIndex,
129128
)
@@ -3846,7 +3845,7 @@ def _validate_positional_slice(self, key: slice) -> None:
38463845
self._validate_indexer("positional", key.stop, "iloc")
38473846
self._validate_indexer("positional", key.step, "iloc")
38483847

3849-
def _convert_slice_indexer(self, key: slice, kind: str_t, is_frame: bool = False):
3848+
def _convert_slice_indexer(self, key: slice, kind: str_t):
38503849
"""
38513850
Convert a slice indexer.
38523851
@@ -3857,9 +3856,6 @@ def _convert_slice_indexer(self, key: slice, kind: str_t, is_frame: bool = False
38573856
----------
38583857
key : label of the slice bound
38593858
kind : {'loc', 'getitem'}
3860-
is_frame : bool, default False
3861-
Whether this is a slice called on DataFrame.__getitem__
3862-
as opposed to Series.__getitem__
38633859
"""
38643860
assert kind in ["loc", "getitem"], kind
38653861

@@ -3882,42 +3878,6 @@ def is_int(v):
38823878

38833879
if kind == "getitem":
38843880
# called from the getitem slicers, validate that we are in fact integers
3885-
if self.is_integer():
3886-
if is_frame:
3887-
# unambiguously positional, no deprecation
3888-
pass
3889-
elif start is None and stop is None:
3890-
# label-based vs positional is irrelevant
3891-
pass
3892-
elif isinstance(self, ABCRangeIndex) and self._range == range(
3893-
len(self)
3894-
):
3895-
# In this case there is no difference between label-based
3896-
# and positional, so nothing will change.
3897-
pass
3898-
elif (
3899-
self.dtype.kind in ["i", "u"]
3900-
and self._is_strictly_monotonic_increasing
3901-
and len(self) > 0
3902-
and self[0] == 0
3903-
and self[-1] == len(self) - 1
3904-
):
3905-
# We are range-like, e.g. created with Index(np.arange(N))
3906-
pass
3907-
elif not is_index_slice:
3908-
# we're going to raise, so don't bother warning, e.g.
3909-
# test_integer_positional_indexing
3910-
pass
3911-
else:
3912-
warnings.warn(
3913-
"The behavior of `series[i:j]` with an integer-dtype index "
3914-
"is deprecated. In a future version, this will be treated "
3915-
"as *label-based* indexing, consistent with e.g. `series[i]` "
3916-
"lookups. To retain the old behavior, use `series.iloc[i:j]`. "
3917-
"To get the future behavior, use `series.loc[i:j]`.",
3918-
FutureWarning,
3919-
stacklevel=find_stack_level(),
3920-
)
39213881
if self.is_integer() or is_index_slice:
39223882
# Note: these checks are redundant if we know is_index_slice
39233883
self._validate_indexer("slice", key.start, "getitem")

pandas/core/indexes/interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def _index_as_unique(self) -> bool:
796796
"cannot handle overlapping indices; use IntervalIndex.get_indexer_non_unique"
797797
)
798798

799-
def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
799+
def _convert_slice_indexer(self, key: slice, kind: str):
800800
if not (key.step is None or key.step == 1):
801801
# GH#31658 if label-based, we require step == 1,
802802
# if positional, we disallow float start/stop
@@ -808,7 +808,7 @@ def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
808808
# i.e. this cannot be interpreted as a positional slice
809809
raise ValueError(msg)
810810

811-
return super()._convert_slice_indexer(key, kind, is_frame=is_frame)
811+
return super()._convert_slice_indexer(key, kind)
812812

813813
@cache_readonly
814814
def _should_fallback_to_positional(self) -> bool:

pandas/core/indexes/numeric.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _should_fallback_to_positional(self) -> bool:
219219
return False
220220

221221
@doc(Index._convert_slice_indexer)
222-
def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
222+
def _convert_slice_indexer(self, key: slice, kind: str):
223223
# TODO(2.0): once #45324 deprecation is enforced we should be able
224224
# to simplify this.
225225
if is_float_dtype(self.dtype):
@@ -231,7 +231,7 @@ def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
231231
# translate to locations
232232
return self.slice_indexer(key.start, key.stop, key.step)
233233

234-
return super()._convert_slice_indexer(key, kind=kind, is_frame=is_frame)
234+
return super()._convert_slice_indexer(key, kind=kind)
235235

236236
@doc(Index._maybe_cast_slice_bound)
237237
def _maybe_cast_slice_bound(self, label, side: str):

pandas/tests/extension/base/getitem.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ def test_get(self, data):
313313
expected = s.iloc[[2, 3]]
314314
self.assert_series_equal(result, expected)
315315

316-
with tm.assert_produces_warning(FutureWarning, match="label-based"):
317-
result = s.get(slice(2))
316+
result = s.get(slice(2))
318317
expected = s.iloc[[0, 1]]
319318
self.assert_series_equal(result, expected)
320319

pandas/tests/indexing/test_floats.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ def test_integer_positional_indexing(self, idx):
337337
"""
338338
s = Series(range(2, 6), index=range(2, 6))
339339

340-
with tm.assert_produces_warning(FutureWarning, match="label-based"):
341-
result = s[2:4]
340+
result = s[2:4]
342341
expected = s.iloc[2:4]
343342
tm.assert_series_equal(result, expected)
344343

pandas/tests/series/indexing/test_get.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def test_get_with_ea(arr):
167167
expected = ser.iloc[[2, 3]]
168168
tm.assert_series_equal(result, expected)
169169

170-
with tm.assert_produces_warning(FutureWarning, match="label-based"):
171-
result = ser.get(slice(2))
170+
result = ser.get(slice(2))
172171
expected = ser.iloc[[0, 1]]
173172
tm.assert_series_equal(result, expected)
174173

pandas/tests/series/indexing/test_getitem.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ def test_getitem_slice_bug(self):
332332
def test_getitem_slice_integers(self):
333333
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])
334334

335-
with tm.assert_produces_warning(FutureWarning, match="label-based"):
336-
result = ser[:4]
335+
result = ser[:4]
337336
expected = Series(ser.values[:4], index=[2, 4, 6, 8])
338337
tm.assert_series_equal(result, expected)
339338

pandas/tests/series/indexing/test_setitem.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,9 @@ def test_setitem_slice(self):
220220
def test_setitem_slice_integers(self):
221221
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])
222222

223-
msg = r"In a future version, this will be treated as \*label-based\* indexing"
224-
with tm.assert_produces_warning(FutureWarning, match=msg):
225-
ser[:4] = 0
226-
with tm.assert_produces_warning(
227-
FutureWarning, match=msg, check_stacklevel=False
228-
):
229-
assert (ser[:4] == 0).all()
230-
with tm.assert_produces_warning(FutureWarning, match=msg):
231-
assert not (ser[4:] == 0).any()
223+
ser[:4] = 0
224+
assert (ser[:4] == 0).all()
225+
assert not (ser[4:] == 0).any()
232226

233227
def test_setitem_slicestep(self):
234228
# caught this bug when writing tests

0 commit comments

Comments
 (0)