Skip to content

Commit

Permalink
DEPR: deprecate allowing slice in DataFrame.take (#51539)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Feb 22, 2023
1 parent 3e2ec50 commit 3f3102b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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
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
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

0 comments on commit 3f3102b

Please sign in to comment.