diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 2c5263f447951..47d20181eb094 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -251,6 +251,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 :class:`DataFrame` reductions ``sum``, ``prod``, ``std``, ``var``, ``sem`` with ``axis=None``, in a future version this will operate over both axes returning a scalar instead of behaving like ``axis=0``; note this also affects numpy functions e.g. ``np.sum(df)`` (:issue:`21597`) - 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`) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 768265a5ce621..33bd56e5b1068 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1175,6 +1175,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 @@ -1240,6 +1245,16 @@ def take( ... fill_value=-10) 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, " + "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)