From 4389096aab764c48ee95b6e2e2e42e3b07f4aa24 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 28 Apr 2023 09:49:49 -0700 Subject: [PATCH 1/2] DEPR: allowing unknowns in take --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/algorithms.py | 14 ++++++++++++++ pandas/tests/test_take.py | 4 +++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 644a7e86ec429..1352639148df5 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -258,6 +258,7 @@ Deprecations - Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`) - Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) - Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) +- Deprecated allowing non-standard inputs in :func:`take`, pass either a ``numpy.ndarray``, :class:`ExtensionArray`, :class:`Index`, or :class:`Series` (:issue:`??`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 4f771b3c80791..fdb77c708bd8f 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1176,6 +1176,11 @@ def take( arr : array-like or scalar value Non array-likes (sequences/scalars without a dtype) are coerced to an ndarray. + + .. deprecated:: 2.1.0 + Passing an argument other than a numpy.ndarray, ExtensionArray, + Index, or Series is deprecated. + indices : sequence of int or one-dimensional np.ndarray of int Indices to be taken. axis : int, default 0 @@ -1241,6 +1246,15 @@ def take( ... fill_value=-10) array([ 10, 10, -10]) """ + if not isinstance(arr, (np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries)): + warnings.warn( + "pd.api.extensions.take accepting non-standard inputs is deprecated " + "and will raise in a future version. Pass either a numpy.ndarray, " + "ExtensionArray, Index, or Series instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + if not is_array_like(arr): arr = np.asarray(arr) diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index 114e28ea4e2a1..cefcf09613de1 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -301,6 +301,8 @@ def test_take_na_empty(self): def test_take_coerces_list(self): arr = [1, 2, 3] - result = algos.take(arr, [0, 0]) + msg = "take accepting non-standard inputs is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = algos.take(arr, [0, 0]) expected = np.array([1, 1]) tm.assert_numpy_array_equal(result, expected) From 51433d98e5044438fcb478dbc2da0abd50e4706c Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 28 Apr 2023 09:51:04 -0700 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/algorithms.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 1352639148df5..3aa43d5489e2f 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -247,6 +247,7 @@ Deprecations - Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`) - Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) - Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`) +- Deprecated allowing non-standard inputs in :func:`take`, pass either a ``numpy.ndarray``, :class:`ExtensionArray`, :class:`Index`, or :class:`Series` (:issue:`52981`) - Deprecated behavior of :func:`concat` when :class:`DataFrame` has columns that are all-NA, in a future version these will not be discarded when determining the resulting dtype (:issue:`40893`) - Deprecated behavior of :meth:`Series.dt.to_pydatetime`, in a future version this will return a :class:`Series` containing python ``datetime`` objects instead of an ``ndarray`` of datetimes; this matches the behavior of other :meth:`Series.dt` properties (:issue:`20306`) - Deprecated logical operations (``|``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``), wrap a sequence in a :class:`Series` or numpy array before operating instead (:issue:`51521`) @@ -258,7 +259,6 @@ Deprecations - Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`) - Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) - Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) -- Deprecated allowing non-standard inputs in :func:`take`, pass either a ``numpy.ndarray``, :class:`ExtensionArray`, :class:`Index`, or :class:`Series` (:issue:`??`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index fdb77c708bd8f..7ae02e877fe17 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1247,6 +1247,7 @@ def take( array([ 10, 10, -10]) """ if not isinstance(arr, (np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries)): + # GH#52981 warnings.warn( "pd.api.extensions.take accepting non-standard inputs is deprecated " "and will raise in a future version. Pass either a numpy.ndarray, "