From ca6e57179708d50c43be2d65ffcde8b0c891607c Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 25 Mar 2024 19:36:51 -0700 Subject: [PATCH 1/3] DEPR: enforce deprecation of non-standard argument to take --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/algorithms.py | 9 +++------ pandas/tests/test_take.py | 12 +++++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a398b93b60018..996195db97ccb 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -202,6 +202,7 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` allowing the ``name`` argument to be a non-tuple when grouping by a list of length 1 (:issue:`54155`) - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - 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 344314d829c19..81adef3c3d90f 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1180,12 +1180,9 @@ 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): 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]) From 122c7c0ec58af61f8aaf840263a8727efaf4ec26 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 26 Mar 2024 10:48:07 -0700 Subject: [PATCH 2/3] Remove no-longer-necessary check --- pandas/core/algorithms.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 555c2609599e3..f82a13ce037b3 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, @@ -1168,9 +1167,6 @@ def take( 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: From c916aa07e944eda1be2aa979a7fc4ca31c7406c3 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 27 Mar 2024 21:06:49 -0700 Subject: [PATCH 3/3] mypy fixup --- pandas/core/algorithms.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index f82a13ce037b3..6a6096567c65d 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1172,12 +1172,20 @@ def take( 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