Skip to content

DEPR: kind kwarg in Index.get_slice_bound, Index.slice_indexer, Index.slice_locs #49265

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 2 commits into from
Oct 24, 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 @@ -180,6 +180,7 @@ Removal of prior version deprecations/changes
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
- Removed argument ``is_copy`` from :meth:`DataFrame.take` and :meth:`Series.take` (:issue:`30615`)
- Removed argument ``kind`` from :meth:`Index.get_slice_bound`, :meth:`Index.slice_indexer` and :meth:`Index.slice_locs` (:issue:`41378`)
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
- Remove keywords ``convert_float`` and ``mangle_dupe_cols`` from :func:`read_excel` (:issue:`41176`)
- Disallow passing non-keyword arguments to :func:`read_excel` except ``io`` and ``sheet_name`` (:issue:`34418`)
Expand Down
30 changes: 3 additions & 27 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6471,7 +6471,6 @@ def slice_indexer(
start: Hashable | None = None,
end: Hashable | None = None,
step: int | None = None,
kind=no_default,
) -> slice:
"""
Compute the slice indexer for input labels and step.
Expand All @@ -6485,9 +6484,6 @@ def slice_indexer(
end : label, default None
If None, defaults to the end.
step : int, default None
kind : str, default None

.. deprecated:: 1.4.0

Returns
-------
Expand All @@ -6514,8 +6510,6 @@ def slice_indexer(
>>> idx.slice_indexer(start='b', end=('c', 'g'))
slice(1, 3, None)
"""
self._deprecated_arg(kind, "kind", "slice_indexer")

start_slice, end_slice = self.slice_locs(start, end, step=step)

# return a slice
Expand Down Expand Up @@ -6550,7 +6544,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t) -> None:
if key is not None and not is_integer(key):
self._raise_invalid_indexer(form, key)

def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
def _maybe_cast_slice_bound(self, label, side: str_t):
"""
This function should be overloaded in subclasses that allow non-trivial
casting on label-slice bounds, e.g. datetime-like indices allowing
Expand All @@ -6560,9 +6554,6 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
----------
label : object
side : {'left', 'right'}
kind : {'loc', 'getitem'} or None

.. deprecated:: 1.3.0

Returns
-------
Expand All @@ -6572,8 +6563,6 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
-----
Value of `side` parameter should be validated in caller.
"""
assert kind in ["loc", "getitem", None, no_default]
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")

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

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

def get_slice_bound(
self, label, side: Literal["left", "right"], kind=no_default
) -> int:
def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
"""
Calculate slice bound that corresponds to given label.

Expand All @@ -6611,17 +6598,12 @@ def get_slice_bound(
----------
label : object
side : {'left', 'right'}
kind : {'loc', 'getitem'} or None

.. deprecated:: 1.4.0

Returns
-------
int
Index of label.
"""
assert kind in ["loc", "getitem", None, no_default]
self._deprecated_arg(kind, "kind", "get_slice_bound")

if side not in ("left", "right"):
raise ValueError(
Expand Down Expand Up @@ -6667,9 +6649,7 @@ def get_slice_bound(
else:
return slc

def slice_locs(
self, start=None, end=None, step=None, kind=no_default
) -> tuple[int, int]:
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
"""
Compute slice locations for input labels.

Expand All @@ -6681,9 +6661,6 @@ def slice_locs(
If None, defaults to the end.
step : int, defaults None
If None, defaults to 1.
kind : {'loc', 'getitem'} or None

.. deprecated:: 1.4.0

Returns
-------
Expand All @@ -6703,7 +6680,6 @@ def slice_locs(
>>> idx.slice_locs(start='b', end='c')
(1, 3)
"""
self._deprecated_arg(kind, "kind", "slice_locs")
inc = step is None or step >= 0

if not inc:
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ def _partial_date_slice(
# try to find the dates
return (lhs_mask & rhs_mask).nonzero()[0]

def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
def _maybe_cast_slice_bound(self, label, side: str):
"""
If label is a string, cast it to scalar type according to resolution.

Parameters
----------
label : object
side : {'left', 'right'}
kind : {'loc', 'getitem'} or None

Returns
-------
Expand All @@ -308,9 +307,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
-----
Value of `side` parameter should be validated in caller.
"""
assert kind in ["loc", "getitem", None, lib.no_default]
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")

if isinstance(label, str):
try:
parsed, reso = self._parse_with_reso(label)
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,19 +714,19 @@ def _maybe_cast_for_get_loc(self, key) -> Timestamp:
return key

@doc(DatetimeTimedeltaMixin._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
def _maybe_cast_slice_bound(self, label, side: str):

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

label = super()._maybe_cast_slice_bound(label, side, kind=kind)
label = super()._maybe_cast_slice_bound(label, side)
self._deprecate_mismatched_indexing(label)
return self._maybe_cast_for_get_loc(label)

def slice_indexer(self, start=None, end=None, step=None, kind=lib.no_default):
def slice_indexer(self, start=None, end=None, step=None):
"""
Return indexer for specified label slice.
Index.slice_indexer, customized to handle time slicing.
Expand All @@ -740,8 +740,6 @@ def slice_indexer(self, start=None, end=None, step=None, kind=lib.no_default):
value-based selection in non-monotonic cases.

"""
self._deprecated_arg(kind, "kind", "slice_indexer")

# For historical reasons DatetimeIndex supports slices between two
# instances of datetime.time as if it were applying a slice mask to
# an array of (self.hour, self.minute, self.seconds, self.microsecond).
Expand All @@ -764,7 +762,7 @@ def check_str_or_none(point) -> bool:
or check_str_or_none(end)
or self.is_monotonic_increasing
):
return Index.slice_indexer(self, start, end, step, kind=kind)
return Index.slice_indexer(self, start, end, step)

mask = np.array(True)
deprecation_mask = np.array(True)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,7 @@ def _should_fallback_to_positional(self) -> bool:
# ExtensionDtype]" has no attribute "subtype"
return self.dtype.subtype.kind in ["m", "M"] # type: ignore[union-attr]

def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")
def _maybe_cast_slice_bound(self, label, side: str):
return getattr(self, side)._maybe_cast_slice_bound(label, side)

def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
Expand Down
14 changes: 1 addition & 13 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,6 @@ def get_slice_bound(
self,
label: Hashable | Sequence[Hashable],
side: Literal["left", "right"],
kind=lib.no_default,
) -> int:
"""
For an ordered MultiIndex, compute slice bound
Expand All @@ -2640,9 +2639,6 @@ def get_slice_bound(
----------
label : object or tuple of objects
side : {'left', 'right'}
kind : {'loc', 'getitem', None}

.. deprecated:: 1.4.0

Returns
-------
Expand Down Expand Up @@ -2675,15 +2671,11 @@ def get_slice_bound(
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
sequence of such.
"""
self._deprecated_arg(kind, "kind", "get_slice_bound")

if not isinstance(label, tuple):
label = (label,)
return self._partial_tup_index(label, side=side)

def slice_locs(
self, start=None, end=None, step=None, kind=lib.no_default
) -> tuple[int, int]:
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
"""
For an ordered MultiIndex, compute the slice locations for input
labels.
Expand All @@ -2700,9 +2692,6 @@ def slice_locs(
If None, defaults to the end
step : int or None
Slice step
kind : string, optional, defaults None

.. deprecated:: 1.4.0

Returns
-------
Expand Down Expand Up @@ -2737,7 +2726,6 @@ def slice_locs(
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
sequence of such.
"""
self._deprecated_arg(kind, "kind", "slice_locs")
# This function adds nothing to its parent implementation (the magic
# happens in get_slice_bound method), but it adds meaningful doc.
return super().slice_locs(start, end, step)
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ def _convert_slice_indexer(self, key: slice, kind: str, is_frame: bool = False):
return super()._convert_slice_indexer(key, kind=kind, is_frame=is_frame)

@doc(Index._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
assert kind in ["loc", "getitem", None, lib.no_default]
self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound")

def _maybe_cast_slice_bound(self, label, side: str):
# we will try to coerce to integers
return self._maybe_cast_indexer(label)

Expand Down
9 changes: 3 additions & 6 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import numpy as np

from pandas._libs import (
index as libindex,
lib,
)
from pandas._libs import index as libindex
from pandas._libs.tslibs import (
BaseOffset,
NaT,
Expand Down Expand Up @@ -474,11 +471,11 @@ def _cast_partial_indexing_scalar(self, label):
return key

@doc(DatetimeIndexOpsMixin._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
def _maybe_cast_slice_bound(self, label, side: str):
if isinstance(label, datetime):
label = self._cast_partial_indexing_scalar(label)

return super()._maybe_cast_slice_bound(label, side, kind=kind)
return super()._maybe_cast_slice_bound(label, side)

def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
iv = Period(parsed, freq=reso.attr_abbrev)
Expand Down
12 changes: 4 additions & 8 deletions pandas/tests/indexes/base_class/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@


class TestGetSliceBounds:
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_within(self, kind, side, expected):
def test_get_slice_bounds_within(self, side, expected):
index = Index(list("abcdef"))
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
result = index.get_slice_bound("e", kind=kind, side=side)
result = index.get_slice_bound("e", side=side)
assert result == expected

@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize(
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)]
)
def test_get_slice_bounds_outside(self, kind, side, expected, data, bound):
def test_get_slice_bounds_outside(self, side, expected, data, bound):
index = Index(data)
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
result = index.get_slice_bound(bound, kind=kind, side=side)
result = index.get_slice_bound(bound, side=side)
assert result == expected

def test_get_slice_bounds_invalid_side(self):
Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,9 @@ def test_maybe_cast_slice_duplicate_monotonic(self):

class TestGetSliceBounds:
@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_datetime_within(
self, box, kind, side, expected, tz_aware_fixture
self, box, side, expected, tz_aware_fixture
):
# GH 35690
tz = tz_aware_fixture
Expand All @@ -725,15 +724,14 @@ def test_get_slice_bounds_datetime_within(
warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
result = index.get_slice_bound(key, side=side)
assert result == expected

@pytest.mark.parametrize("box", [datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)])
def test_get_slice_bounds_datetime_outside(
self, box, kind, side, year, expected, tz_aware_fixture
self, box, side, year, expected, tz_aware_fixture
):
# GH 35690
tz = tz_aware_fixture
Expand All @@ -743,12 +741,11 @@ def test_get_slice_bounds_datetime_outside(
warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
result = index.get_slice_bound(key, side=side)
assert result == expected

@pytest.mark.parametrize("box", [datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
def test_slice_datetime_locs(self, box, tz_aware_fixture):
# GH 34077
tz = tz_aware_fixture
index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz)
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,7 @@ def test_timestamp_multiindex_indexer():
def test_get_slice_bound_with_missing_value(index_arr, expected, target, algo):
# issue 19132
idx = MultiIndex.from_arrays(index_arr)
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
result = idx.get_slice_bound(target, side=algo, kind="loc")
result = idx.get_slice_bound(target, side=algo)
assert result == expected


Expand Down
13 changes: 4 additions & 9 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,20 +576,15 @@ def test_slice_locs_na_raises(self):


class TestGetSliceBounds:
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_within(self, kind, side, expected):
def test_get_slice_bounds_within(self, side, expected):
index = Index(range(6))
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):

result = index.get_slice_bound(4, kind=kind, side=side)
result = index.get_slice_bound(4, side=side)
assert result == expected

@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize("bound, expected", [(-1, 0), (10, 6)])
def test_get_slice_bounds_outside(self, kind, side, expected, bound):
def test_get_slice_bounds_outside(self, side, expected, bound):
index = Index(range(6))
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
result = index.get_slice_bound(bound, kind=kind, side=side)
result = index.get_slice_bound(bound, side=side)
assert result == expected
Loading