From ff6d918a88ef05d0f07abed930e912fe30a9c935 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 21 Feb 2023 08:19:05 -0800 Subject: [PATCH 1/3] DEPR: deprecate allowing slice in DataFrame.take --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/frame.py | 3 +++ pandas/core/generic.py | 7 +++++++ pandas/tests/frame/indexing/test_take.py | 9 +++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 1c139f4692b52..7b06543e47d44 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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:`??`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4a6b95f9a2e11..10e64f7fbd52c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7c2e42ef71d46..b732c00fe4eca 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3910,6 +3910,13 @@ class max_speed "not slice." ) else: + warnings.warn( + "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 diff --git a/pandas/tests/frame/indexing/test_take.py b/pandas/tests/frame/indexing/test_take.py index a3d13ecd7b98e..5538c50784462 100644 --- a/pandas/tests/frame/indexing/test_take.py +++ b/pandas/tests/frame/indexing/test_take.py @@ -4,6 +4,15 @@ class TestDataFrameTake: + def test_take_slices_deprecated(self, float_frame): + 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] From 5d049868664118365876d7dd6e9ecc6d68558015 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 21 Feb 2023 08:20:44 -0800 Subject: [PATCH 2/3] GH ref --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/generic.py | 1 + pandas/tests/frame/indexing/test_take.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7b06543e47d44..b83f317814ad9 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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:`??`) +- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b732c00fe4eca..d85dbce8faa81 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3911,6 +3911,7 @@ class max_speed ) 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.", diff --git a/pandas/tests/frame/indexing/test_take.py b/pandas/tests/frame/indexing/test_take.py index 5538c50784462..8c17231440917 100644 --- a/pandas/tests/frame/indexing/test_take.py +++ b/pandas/tests/frame/indexing/test_take.py @@ -5,6 +5,7 @@ class TestDataFrameTake: def test_take_slices_deprecated(self, float_frame): + # GH#51539 df = float_frame slc = slice(0, 4, 1) From dd410748bf08fe00fde0b7d0e66a78d10b785f17 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 21 Feb 2023 10:53:18 -0800 Subject: [PATCH 3/3] Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d85dbce8faa81..33dc62564d34e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3912,7 +3912,7 @@ class max_speed else: warnings.warn( # GH#51539 - "Passing a slice to {type(self).__name__}.take is deprecated " + 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,