Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Other API changes

Deprecations
~~~~~~~~~~~~
-
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3749,6 +3749,9 @@ def __getitem__(self, key):
if getattr(indexer, "dtype", None) == bool:
indexer = np.where(indexer)[0]

if isinstance(indexer, slice):
return self._slice(indexer, axis=1)

data = self._take_with_is_copy(indexer, axis=1)

if is_single_key:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3910,6 +3910,14 @@ class max_speed
"not slice."
)
else:
warnings.warn(
# GH#51539
"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
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/indexing/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@


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

slc = slice(0, 4, 1)
with tm.assert_produces_warning(FutureWarning):
df.take(slc, axis=0)
with tm.assert_produces_warning(FutureWarning):
df.take(slc, axis=1)

def test_take(self, float_frame):
# homogeneous
order = [3, 1, 2, 0]
Expand Down