Skip to content

Commit a2e6ddc

Browse files
authored
CLN: int slice on Index[float] as positional (#58646)
1 parent 7982c60 commit a2e6ddc

File tree

5 files changed

+11
-40
lines changed

5 files changed

+11
-40
lines changed

doc/source/whatsnew/v0.13.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ Float64Index API change
345345
.. ipython:: python
346346
:okwarning:
347347
348-
s[2:4]
349348
s.loc[2:4]
350349
s.iloc[2:4]
351350

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Removal of prior version deprecations/changes
225225
- All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`)
226226
- All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`)
227227
- Changed behavior of :meth:`Series.__getitem__` and :meth:`Series.__setitem__` to always treat integer keys as labels, never as positional, consistent with :class:`DataFrame` behavior (:issue:`50617`)
228+
- Changed behavior of :meth:`Series.__getitem__`, :meth:`Series.__setitem__`, :meth:`DataFrame.__getitem__`, :meth:`DataFrame.__setitem__` with an integer slice on objects with a floating-dtype index. This is now treated as *positional* indexing (:issue:`49612`)
228229
- Disallow a callable argument to :meth:`Series.iloc` to return a ``tuple`` (:issue:`53769`)
229230
- Disallow allowing logical operations (``||``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``); wrap the objects in :class:`Series`, :class:`Index`, or ``np.array`` first instead (:issue:`52264`)
230231
- Disallow automatic casting to object in :class:`Series` logical operations (``&``, ``^``, ``||``) between series with mismatched indexes and dtypes other than ``object`` or ``bool`` (:issue:`52538`)

pandas/core/indexes/base.py

-19
Original file line numberDiff line numberDiff line change
@@ -4005,25 +4005,6 @@ def _convert_slice_indexer(self, key: slice, kind: Literal["loc", "getitem"]):
40054005

40064006
# TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able
40074007
# to simplify this.
4008-
if lib.is_np_dtype(self.dtype, "f"):
4009-
# We always treat __getitem__ slicing as label-based
4010-
# translate to locations
4011-
if kind == "getitem" and is_index_slice and not start == stop and step != 0:
4012-
# exclude step=0 from the warning because it will raise anyway
4013-
# start/stop both None e.g. [:] or [::-1] won't change.
4014-
# exclude start==stop since it will be empty either way, or
4015-
# will be [:] or [::-1] which won't change
4016-
warnings.warn(
4017-
# GH#49612
4018-
"The behavior of obj[i:j] with a float-dtype index is "
4019-
"deprecated. In a future version, this will be treated as "
4020-
"positional instead of label-based. For label-based slicing, "
4021-
"use obj.loc[i:j] instead",
4022-
FutureWarning,
4023-
stacklevel=find_stack_level(),
4024-
)
4025-
return self.slice_indexer(start, stop, step)
4026-
40274008
if kind == "getitem":
40284009
# called from the getitem slicers, validate that we are in fact integers
40294010
if is_index_slice:

pandas/tests/frame/indexing/test_indexing.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,14 @@ def test_getitem_setitem_boolean_multi(self):
724724
expected.loc[[0, 2], [1]] = 5
725725
tm.assert_frame_equal(df, expected)
726726

727+
def test_getitem_float_label_positional(self):
728+
# GH 53338
729+
index = Index([1.5, 2])
730+
df = DataFrame(range(2), index=index)
731+
result = df[1:2]
732+
expected = DataFrame([1], index=[2.0])
733+
tm.assert_frame_equal(result, expected)
734+
727735
def test_getitem_setitem_float_labels(self):
728736
index = Index([1.5, 2, 3, 4, 5])
729737
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)), index=index)
@@ -748,12 +756,6 @@ def test_getitem_setitem_float_labels(self):
748756
expected = df.iloc[0:2]
749757
tm.assert_frame_equal(result, expected)
750758

751-
expected = df.iloc[0:2]
752-
msg = r"The behavior of obj\[i:j\] with a float-dtype index"
753-
with tm.assert_produces_warning(FutureWarning, match=msg):
754-
result = df[1:2]
755-
tm.assert_frame_equal(result, expected)
756-
757759
# #2727
758760
index = Index([1.0, 2.5, 3.5, 4.5, 5.0])
759761
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)), index=index)

pandas/tests/indexing/test_floats.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -488,24 +488,12 @@ def test_floating_misc(self, indexer_sl):
488488
for fancy_idx in [[5, 0], np.array([5, 0])]:
489489
tm.assert_series_equal(indexer_sl(s)[fancy_idx], expected)
490490

491-
warn = FutureWarning if indexer_sl is tm.setitem else None
492-
msg = r"The behavior of obj\[i:j\] with a float-dtype index"
493-
494491
# all should return the same as we are slicing 'the same'
495-
with tm.assert_produces_warning(warn, match=msg):
496-
result1 = indexer_sl(s)[2:5]
497492
result2 = indexer_sl(s)[2.0:5.0]
498493
result3 = indexer_sl(s)[2.0:5]
499494
result4 = indexer_sl(s)[2.1:5]
500-
tm.assert_series_equal(result1, result2)
501-
tm.assert_series_equal(result1, result3)
502-
tm.assert_series_equal(result1, result4)
503-
504-
expected = Series([1, 2], index=[2.5, 5.0])
505-
with tm.assert_produces_warning(warn, match=msg):
506-
result = indexer_sl(s)[2:5]
507-
508-
tm.assert_series_equal(result, expected)
495+
tm.assert_series_equal(result2, result3)
496+
tm.assert_series_equal(result2, result4)
509497

510498
# list selection
511499
result1 = indexer_sl(s)[[0.0, 5, 10]]

0 commit comments

Comments
 (0)