Skip to content

Commit 3f3102b

Browse files
authored
DEPR: deprecate allowing slice in DataFrame.take (#51539)
1 parent 3e2ec50 commit 3f3102b

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Other API changes
9292

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

9898
.. ---------------------------------------------------------------------------

pandas/core/frame.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,6 +3749,9 @@ def __getitem__(self, key):
37493749
if getattr(indexer, "dtype", None) == bool:
37503750
indexer = np.where(indexer)[0]
37513751

3752+
if isinstance(indexer, slice):
3753+
return self._slice(indexer, axis=1)
3754+
37523755
data = self._take_with_is_copy(indexer, axis=1)
37533756

37543757
if is_single_key:

pandas/core/generic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,6 +3910,14 @@ class max_speed
39103910
"not slice."
39113911
)
39123912
else:
3913+
warnings.warn(
3914+
# GH#51539
3915+
f"Passing a slice to {type(self).__name__}.take is deprecated "
3916+
"and will raise in a future version. Use `obj[slicer]` or pass "
3917+
"a sequence of integers instead.",
3918+
FutureWarning,
3919+
stacklevel=find_stack_level(),
3920+
)
39133921
# We can get here with a slice via DataFrame.__getitem__
39143922
indices = np.arange(
39153923
indices.start, indices.stop, indices.step, dtype=np.intp

pandas/tests/frame/indexing/test_take.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55

66
class TestDataFrameTake:
7+
def test_take_slices_deprecated(self, float_frame):
8+
# GH#51539
9+
df = float_frame
10+
11+
slc = slice(0, 4, 1)
12+
with tm.assert_produces_warning(FutureWarning):
13+
df.take(slc, axis=0)
14+
with tm.assert_produces_warning(FutureWarning):
15+
df.take(slc, axis=1)
16+
717
def test_take(self, float_frame):
818
# homogeneous
919
order = [3, 1, 2, 0]

0 commit comments

Comments
 (0)