Skip to content

Remove support for slices in take #57343

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
Feb 13, 2024
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Removal of prior version deprecations/changes
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
- Removed support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`)
- Removed support for ``slice`` in :meth:`DataFrame.take` (:issue:`51539`)
- Removed the ``ArrayManager`` (:issue:`55043`)
- Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`)
- Removed the ``is_boolean``, ``is_integer``, ``is_floating``, ``holds_integer``, ``is_numeric``, ``is_categorical``, ``is_object``, and ``is_interval`` attributes of :class:`Index` (:issue:`50042`)
Expand Down
22 changes: 4 additions & 18 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3851,28 +3851,14 @@ class max_speed

nv.validate_take((), kwargs)

if not isinstance(indices, slice):
indices = np.asarray(indices, dtype=np.intp)
if axis == 0 and indices.ndim == 1 and is_range_indexer(indices, len(self)):
return self.copy(deep=False)
elif self.ndim == 1:
if isinstance(indices, slice):
raise TypeError(
f"{type(self).__name__}.take requires a sequence of integers, "
"not slice."
)
else:
warnings.warn(
# GH#51539
f"Passing a slice to {type(self).__name__}.take is deprecated "
"and will raise in a future version. Use `obj[slicer]` or pass "
"a sequence of integers instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
# We can get here with a slice via DataFrame.__getitem__
indices = np.arange(
indices.start, indices.stop, indices.step, dtype=np.intp
)
indices = np.asarray(indices, dtype=np.intp)
if axis == 0 and indices.ndim == 1 and is_range_indexer(indices, len(self)):
return self.copy(deep=False)

new_data = self._mgr.take(
indices,
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/indexing/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@


class TestDataFrameTake:
def test_take_slices_deprecated(self, float_frame):
def test_take_slices_not_supported(self, float_frame):
# GH#51539
df = float_frame

slc = slice(0, 4, 1)
with tm.assert_produces_warning(FutureWarning):
with pytest.raises(TypeError, match="slice"):
df.take(slc, axis=0)
with tm.assert_produces_warning(FutureWarning):
with pytest.raises(TypeError, match="slice"):
df.take(slc, axis=1)

def test_take(self, float_frame):
Expand Down