Skip to content

Commit

Permalink
Remove support for slices in take (pandas-dev#57343)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Feb 13, 2024
1 parent 590d6ed commit 3475d83
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
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 @@ -133,6 +133,7 @@ Removal of prior version deprecations/changes
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
- Removed support for :class:`DataFrame` in :meth:`DataFrame.from_records`(:issue:`51697`)
- 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

0 comments on commit 3475d83

Please sign in to comment.