diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 549d49aaa1853..547055082ced3 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -218,6 +218,7 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - Enforced deprecation of :meth:`offsets.Tick.delta`, use ``pd.Timedelta(obj)`` instead (:issue:`55498`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) +- Enforced deprecation of non-standard (``np.ndarray``, :class:`ExtensionArray`, :class:`Index`, or :class:`Series`) argument to :func:`api.extensions.take` (:issue:`52981`) - Enforced deprecation of parsing system timezone strings to ``tzlocal``, which depended on system timezone, pass the 'tz' keyword instead (:issue:`50791`) - Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`) - Enforced deprecation of string ``AS`` denoting frequency in :class:`YearBegin` and strings ``AS-DEC``, ``AS-JAN``, etc. denoting annual frequencies with various fiscal year starts (:issue:`57793`) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 8620aafd97528..6a6096567c65d 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -43,7 +43,6 @@ ensure_float64, ensure_object, ensure_platform_int, - is_array_like, is_bool_dtype, is_complex_dtype, is_dict_like, @@ -1163,28 +1162,30 @@ def take( """ 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(), + raise TypeError( + "pd.api.extensions.take requires a numpy.ndarray, " + f"ExtensionArray, Index, or Series, got {type(arr).__name__}." ) - if not is_array_like(arr): - arr = np.asarray(arr) - indices = ensure_platform_int(indices) if allow_fill: # Pandas style, -1 means NA validate_indices(indices, arr.shape[axis]) + # error: Argument 1 to "take_nd" has incompatible type + # "ndarray[Any, Any] | ExtensionArray | Index | Series"; expected + # "ndarray[Any, Any]" result = take_nd( - arr, indices, axis=axis, allow_fill=True, fill_value=fill_value + arr, # type: ignore[arg-type] + indices, + axis=axis, + allow_fill=True, + fill_value=fill_value, ) else: # NumPy style - result = arr.take(indices, axis=axis) + # error: Unexpected keyword argument "axis" for "take" of "ExtensionArray" + result = arr.take(indices, axis=axis) # type: ignore[call-arg,assignment] return result diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index 4f34ab34c35f0..ce2e4e0f6cec5 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -299,9 +299,11 @@ def test_take_na_empty(self): tm.assert_numpy_array_equal(result, expected) def test_take_coerces_list(self): + # GH#52981 coercing is deprecated, disabled in 3.0 arr = [1, 2, 3] - 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) + msg = ( + "pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, " + "Index, or Series, got list" + ) + with pytest.raises(TypeError, match=msg): + algos.take(arr, [0, 0])