Skip to content

Commit 86c3d42

Browse files
authored
DEPR: kind kwarg in Index.get_slice_bound, Index.slice_indexer, Index.slice_locs (#49265)
* deprecate kind kwarg in get_slice_bound * whatsnew
1 parent 54b8f60 commit 86c3d42

File tree

13 files changed

+29
-103
lines changed

13 files changed

+29
-103
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Removal of prior version deprecations/changes
182182
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)
183183
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
184184
- Removed argument ``is_copy`` from :meth:`DataFrame.take` and :meth:`Series.take` (:issue:`30615`)
185+
- Removed argument ``kind`` from :meth:`Index.get_slice_bound`, :meth:`Index.slice_indexer` and :meth:`Index.slice_locs` (:issue:`41378`)
185186
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
186187
- Remove keywords ``convert_float`` and ``mangle_dupe_cols`` from :func:`read_excel` (:issue:`41176`)
187188
- Disallow passing non-keyword arguments to :func:`read_excel` except ``io`` and ``sheet_name`` (:issue:`34418`)

pandas/core/indexes/base.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6447,7 +6447,6 @@ def slice_indexer(
64476447
start: Hashable | None = None,
64486448
end: Hashable | None = None,
64496449
step: int | None = None,
6450-
kind=no_default,
64516450
) -> slice:
64526451
"""
64536452
Compute the slice indexer for input labels and step.
@@ -6461,9 +6460,6 @@ def slice_indexer(
64616460
end : label, default None
64626461
If None, defaults to the end.
64636462
step : int, default None
6464-
kind : str, default None
6465-
6466-
.. deprecated:: 1.4.0
64676463
64686464
Returns
64696465
-------
@@ -6490,8 +6486,6 @@ def slice_indexer(
64906486
>>> idx.slice_indexer(start='b', end=('c', 'g'))
64916487
slice(1, 3, None)
64926488
"""
6493-
self._deprecated_arg(kind, "kind", "slice_indexer")
6494-
64956489
start_slice, end_slice = self.slice_locs(start, end, step=step)
64966490

64976491
# return a slice
@@ -6526,7 +6520,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t) -> None:
65266520
if key is not None and not is_integer(key):
65276521
self._raise_invalid_indexer(form, key)
65286522

6529-
def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
6523+
def _maybe_cast_slice_bound(self, label, side: str_t):
65306524
"""
65316525
This function should be overloaded in subclasses that allow non-trivial
65326526
casting on label-slice bounds, e.g. datetime-like indices allowing
@@ -6536,9 +6530,6 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
65366530
----------
65376531
label : object
65386532
side : {'left', 'right'}
6539-
kind : {'loc', 'getitem'} or None
6540-
6541-
.. deprecated:: 1.3.0
65426533
65436534
Returns
65446535
-------
@@ -6548,8 +6539,6 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
65486539
-----
65496540
Value of `side` parameter should be validated in caller.
65506541
"""
6551-
assert kind in ["loc", "getitem", None, no_default]
6552-
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
65536542

65546543
# We are a plain index here (sub-class override this method if they
65556544
# wish to have special treatment for floats/ints, e.g. Float64Index and
@@ -6574,9 +6563,7 @@ def _searchsorted_monotonic(self, label, side: Literal["left", "right"] = "left"
65746563

65756564
raise ValueError("index must be monotonic increasing or decreasing")
65766565

6577-
def get_slice_bound(
6578-
self, label, side: Literal["left", "right"], kind=no_default
6579-
) -> int:
6566+
def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
65806567
"""
65816568
Calculate slice bound that corresponds to given label.
65826569
@@ -6587,17 +6574,12 @@ def get_slice_bound(
65876574
----------
65886575
label : object
65896576
side : {'left', 'right'}
6590-
kind : {'loc', 'getitem'} or None
6591-
6592-
.. deprecated:: 1.4.0
65936577
65946578
Returns
65956579
-------
65966580
int
65976581
Index of label.
65986582
"""
6599-
assert kind in ["loc", "getitem", None, no_default]
6600-
self._deprecated_arg(kind, "kind", "get_slice_bound")
66016583

66026584
if side not in ("left", "right"):
66036585
raise ValueError(
@@ -6643,9 +6625,7 @@ def get_slice_bound(
66436625
else:
66446626
return slc
66456627

6646-
def slice_locs(
6647-
self, start=None, end=None, step=None, kind=no_default
6648-
) -> tuple[int, int]:
6628+
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
66496629
"""
66506630
Compute slice locations for input labels.
66516631
@@ -6657,9 +6637,6 @@ def slice_locs(
66576637
If None, defaults to the end.
66586638
step : int, defaults None
66596639
If None, defaults to 1.
6660-
kind : {'loc', 'getitem'} or None
6661-
6662-
.. deprecated:: 1.4.0
66636640
66646641
Returns
66656642
-------
@@ -6679,7 +6656,6 @@ def slice_locs(
66796656
>>> idx.slice_locs(start='b', end='c')
66806657
(1, 3)
66816658
"""
6682-
self._deprecated_arg(kind, "kind", "slice_locs")
66836659
inc = step is None or step >= 0
66846660

66856661
if not inc:

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,14 @@ def _partial_date_slice(
290290
# try to find the dates
291291
return (lhs_mask & rhs_mask).nonzero()[0]
292292

293-
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
293+
def _maybe_cast_slice_bound(self, label, side: str):
294294
"""
295295
If label is a string, cast it to scalar type according to resolution.
296296
297297
Parameters
298298
----------
299299
label : object
300300
side : {'left', 'right'}
301-
kind : {'loc', 'getitem'} or None
302301
303302
Returns
304303
-------
@@ -308,9 +307,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
308307
-----
309308
Value of `side` parameter should be validated in caller.
310309
"""
311-
assert kind in ["loc", "getitem", None, lib.no_default]
312-
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
313-
314310
if isinstance(label, str):
315311
try:
316312
parsed, reso = self._parse_with_reso(label)

pandas/core/indexes/datetimes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,19 +714,19 @@ def _maybe_cast_for_get_loc(self, key) -> Timestamp:
714714
return key
715715

716716
@doc(DatetimeTimedeltaMixin._maybe_cast_slice_bound)
717-
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
717+
def _maybe_cast_slice_bound(self, label, side: str):
718718

719719
# GH#42855 handle date here instead of get_slice_bound
720720
if isinstance(label, date) and not isinstance(label, datetime):
721721
# Pandas supports slicing with dates, treated as datetimes at midnight.
722722
# https://github.com/pandas-dev/pandas/issues/31501
723723
label = Timestamp(label).to_pydatetime()
724724

725-
label = super()._maybe_cast_slice_bound(label, side, kind=kind)
725+
label = super()._maybe_cast_slice_bound(label, side)
726726
self._deprecate_mismatched_indexing(label)
727727
return self._maybe_cast_for_get_loc(label)
728728

729-
def slice_indexer(self, start=None, end=None, step=None, kind=lib.no_default):
729+
def slice_indexer(self, start=None, end=None, step=None):
730730
"""
731731
Return indexer for specified label slice.
732732
Index.slice_indexer, customized to handle time slicing.
@@ -740,8 +740,6 @@ def slice_indexer(self, start=None, end=None, step=None, kind=lib.no_default):
740740
value-based selection in non-monotonic cases.
741741
742742
"""
743-
self._deprecated_arg(kind, "kind", "slice_indexer")
744-
745743
# For historical reasons DatetimeIndex supports slices between two
746744
# instances of datetime.time as if it were applying a slice mask to
747745
# an array of (self.hour, self.minute, self.seconds, self.microsecond).
@@ -764,7 +762,7 @@ def check_str_or_none(point) -> bool:
764762
or check_str_or_none(end)
765763
or self.is_monotonic_increasing
766764
):
767-
return Index.slice_indexer(self, start, end, step, kind=kind)
765+
return Index.slice_indexer(self, start, end, step)
768766

769767
mask = np.array(True)
770768
deprecation_mask = np.array(True)

pandas/core/indexes/interval.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ def _should_fallback_to_positional(self) -> bool:
786786
# ExtensionDtype]" has no attribute "subtype"
787787
return self.dtype.subtype.kind in ["m", "M"] # type: ignore[union-attr]
788788

789-
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
790-
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
789+
def _maybe_cast_slice_bound(self, label, side: str):
791790
return getattr(self, side)._maybe_cast_slice_bound(label, side)
792791

793792
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:

pandas/core/indexes/multi.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,6 @@ def get_slice_bound(
26102610
self,
26112611
label: Hashable | Sequence[Hashable],
26122612
side: Literal["left", "right"],
2613-
kind=lib.no_default,
26142613
) -> int:
26152614
"""
26162615
For an ordered MultiIndex, compute slice bound
@@ -2623,9 +2622,6 @@ def get_slice_bound(
26232622
----------
26242623
label : object or tuple of objects
26252624
side : {'left', 'right'}
2626-
kind : {'loc', 'getitem', None}
2627-
2628-
.. deprecated:: 1.4.0
26292625
26302626
Returns
26312627
-------
@@ -2658,15 +2654,11 @@ def get_slice_bound(
26582654
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
26592655
sequence of such.
26602656
"""
2661-
self._deprecated_arg(kind, "kind", "get_slice_bound")
2662-
26632657
if not isinstance(label, tuple):
26642658
label = (label,)
26652659
return self._partial_tup_index(label, side=side)
26662660

2667-
def slice_locs(
2668-
self, start=None, end=None, step=None, kind=lib.no_default
2669-
) -> tuple[int, int]:
2661+
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
26702662
"""
26712663
For an ordered MultiIndex, compute the slice locations for input
26722664
labels.
@@ -2683,9 +2675,6 @@ def slice_locs(
26832675
If None, defaults to the end
26842676
step : int or None
26852677
Slice step
2686-
kind : string, optional, defaults None
2687-
2688-
.. deprecated:: 1.4.0
26892678
26902679
Returns
26912680
-------
@@ -2720,7 +2709,6 @@ def slice_locs(
27202709
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
27212710
sequence of such.
27222711
"""
2723-
self._deprecated_arg(kind, "kind", "slice_locs")
27242712
# This function adds nothing to its parent implementation (the magic
27252713
# happens in get_slice_bound method), but it adds meaningful doc.
27262714
return super().slice_locs(start, end, step)

pandas/core/indexes/numeric.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
233233
return super()._convert_slice_indexer(key, kind=kind, is_frame=is_frame)
234234

235235
@doc(Index._maybe_cast_slice_bound)
236-
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
237-
assert kind in ["loc", "getitem", None, lib.no_default]
238-
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
239-
236+
def _maybe_cast_slice_bound(self, label, side: str):
240237
# we will try to coerce to integers
241238
return self._maybe_cast_indexer(label)
242239

pandas/core/indexes/period.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
import numpy as np
1010

11-
from pandas._libs import (
12-
index as libindex,
13-
lib,
14-
)
11+
from pandas._libs import index as libindex
1512
from pandas._libs.tslibs import (
1613
BaseOffset,
1714
NaT,
@@ -474,11 +471,11 @@ def _cast_partial_indexing_scalar(self, label):
474471
return key
475472

476473
@doc(DatetimeIndexOpsMixin._maybe_cast_slice_bound)
477-
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
474+
def _maybe_cast_slice_bound(self, label, side: str):
478475
if isinstance(label, datetime):
479476
label = self._cast_partial_indexing_scalar(label)
480477

481-
return super()._maybe_cast_slice_bound(label, side, kind=kind)
478+
return super()._maybe_cast_slice_bound(label, side)
482479

483480
def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
484481
iv = Period(parsed, freq=reso.attr_abbrev)

pandas/tests/indexes/base_class/test_indexing.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@
1010

1111

1212
class TestGetSliceBounds:
13-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
1413
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
15-
def test_get_slice_bounds_within(self, kind, side, expected):
14+
def test_get_slice_bounds_within(self, side, expected):
1615
index = Index(list("abcdef"))
17-
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
18-
result = index.get_slice_bound("e", kind=kind, side=side)
16+
result = index.get_slice_bound("e", side=side)
1917
assert result == expected
2018

21-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
2219
@pytest.mark.parametrize("side", ["left", "right"])
2320
@pytest.mark.parametrize(
2421
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)]
2522
)
26-
def test_get_slice_bounds_outside(self, kind, side, expected, data, bound):
23+
def test_get_slice_bounds_outside(self, side, expected, data, bound):
2724
index = Index(data)
28-
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
29-
result = index.get_slice_bound(bound, kind=kind, side=side)
25+
result = index.get_slice_bound(bound, side=side)
3026
assert result == expected
3127

3228
def test_get_slice_bounds_invalid_side(self):

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,9 @@ def test_maybe_cast_slice_duplicate_monotonic(self):
712712

713713
class TestGetSliceBounds:
714714
@pytest.mark.parametrize("box", [date, datetime, Timestamp])
715-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
716715
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
717716
def test_get_slice_bounds_datetime_within(
718-
self, box, kind, side, expected, tz_aware_fixture
717+
self, box, side, expected, tz_aware_fixture
719718
):
720719
# GH 35690
721720
tz = tz_aware_fixture
@@ -725,15 +724,14 @@ def test_get_slice_bounds_datetime_within(
725724
warn = None if tz is None else FutureWarning
726725
with tm.assert_produces_warning(warn):
727726
# GH#36148 will require tzawareness-compat
728-
result = index.get_slice_bound(key, kind=kind, side=side)
727+
result = index.get_slice_bound(key, side=side)
729728
assert result == expected
730729

731730
@pytest.mark.parametrize("box", [datetime, Timestamp])
732-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
733731
@pytest.mark.parametrize("side", ["left", "right"])
734732
@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)])
735733
def test_get_slice_bounds_datetime_outside(
736-
self, box, kind, side, year, expected, tz_aware_fixture
734+
self, box, side, year, expected, tz_aware_fixture
737735
):
738736
# GH 35690
739737
tz = tz_aware_fixture
@@ -743,12 +741,11 @@ def test_get_slice_bounds_datetime_outside(
743741
warn = None if tz is None else FutureWarning
744742
with tm.assert_produces_warning(warn):
745743
# GH#36148 will require tzawareness-compat
746-
result = index.get_slice_bound(key, kind=kind, side=side)
744+
result = index.get_slice_bound(key, side=side)
747745
assert result == expected
748746

749747
@pytest.mark.parametrize("box", [datetime, Timestamp])
750-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
751-
def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
748+
def test_slice_datetime_locs(self, box, tz_aware_fixture):
752749
# GH 34077
753750
tz = tz_aware_fixture
754751
index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz)

pandas/tests/indexes/multi/test_indexing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,7 @@ def test_timestamp_multiindex_indexer():
841841
def test_get_slice_bound_with_missing_value(index_arr, expected, target, algo):
842842
# issue 19132
843843
idx = MultiIndex.from_arrays(index_arr)
844-
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
845-
result = idx.get_slice_bound(target, side=algo, kind="loc")
844+
result = idx.get_slice_bound(target, side=algo)
846845
assert result == expected
847846

848847

pandas/tests/indexes/numeric/test_indexing.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,20 +576,15 @@ def test_slice_locs_na_raises(self):
576576

577577

578578
class TestGetSliceBounds:
579-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
580579
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
581-
def test_get_slice_bounds_within(self, kind, side, expected):
580+
def test_get_slice_bounds_within(self, side, expected):
582581
index = Index(range(6))
583-
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
584-
585-
result = index.get_slice_bound(4, kind=kind, side=side)
582+
result = index.get_slice_bound(4, side=side)
586583
assert result == expected
587584

588-
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
589585
@pytest.mark.parametrize("side", ["left", "right"])
590586
@pytest.mark.parametrize("bound, expected", [(-1, 0), (10, 6)])
591-
def test_get_slice_bounds_outside(self, kind, side, expected, bound):
587+
def test_get_slice_bounds_outside(self, side, expected, bound):
592588
index = Index(range(6))
593-
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
594-
result = index.get_slice_bound(bound, kind=kind, side=side)
589+
result = index.get_slice_bound(bound, side=side)
595590
assert result == expected

0 commit comments

Comments
 (0)