Skip to content

Commit 8cb96e7

Browse files
authored
DEPR: enforce deprecation of non-standard argument to take (#58011)
* DEPR: enforce deprecation of non-standard argument to take * Remove no-longer-necessary check * mypy fixup
1 parent aba759d commit 8cb96e7

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Removal of prior version deprecations/changes
221221
- Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`)
222222
- Enforced deprecation of :meth:`offsets.Tick.delta`, use ``pd.Timedelta(obj)`` instead (:issue:`55498`)
223223
- 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`)
224+
- Enforced deprecation of non-standard (``np.ndarray``, :class:`ExtensionArray`, :class:`Index`, or :class:`Series`) argument to :func:`api.extensions.take` (:issue:`52981`)
224225
- Enforced deprecation of parsing system timezone strings to ``tzlocal``, which depended on system timezone, pass the 'tz' keyword instead (:issue:`50791`)
225226
- Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`)
226227
- 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`)

pandas/core/algorithms.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
ensure_float64,
4444
ensure_object,
4545
ensure_platform_int,
46-
is_array_like,
4746
is_bool_dtype,
4847
is_complex_dtype,
4948
is_dict_like,
@@ -1163,28 +1162,30 @@ def take(
11631162
"""
11641163
if not isinstance(arr, (np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries)):
11651164
# GH#52981
1166-
warnings.warn(
1167-
"pd.api.extensions.take accepting non-standard inputs is deprecated "
1168-
"and will raise in a future version. Pass either a numpy.ndarray, "
1169-
"ExtensionArray, Index, or Series instead.",
1170-
FutureWarning,
1171-
stacklevel=find_stack_level(),
1165+
raise TypeError(
1166+
"pd.api.extensions.take requires a numpy.ndarray, "
1167+
f"ExtensionArray, Index, or Series, got {type(arr).__name__}."
11721168
)
11731169

1174-
if not is_array_like(arr):
1175-
arr = np.asarray(arr)
1176-
11771170
indices = ensure_platform_int(indices)
11781171

11791172
if allow_fill:
11801173
# Pandas style, -1 means NA
11811174
validate_indices(indices, arr.shape[axis])
1175+
# error: Argument 1 to "take_nd" has incompatible type
1176+
# "ndarray[Any, Any] | ExtensionArray | Index | Series"; expected
1177+
# "ndarray[Any, Any]"
11821178
result = take_nd(
1183-
arr, indices, axis=axis, allow_fill=True, fill_value=fill_value
1179+
arr, # type: ignore[arg-type]
1180+
indices,
1181+
axis=axis,
1182+
allow_fill=True,
1183+
fill_value=fill_value,
11841184
)
11851185
else:
11861186
# NumPy style
1187-
result = arr.take(indices, axis=axis)
1187+
# error: Unexpected keyword argument "axis" for "take" of "ExtensionArray"
1188+
result = arr.take(indices, axis=axis) # type: ignore[call-arg,assignment]
11881189
return result
11891190

11901191

pandas/tests/test_take.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,11 @@ def test_take_na_empty(self):
299299
tm.assert_numpy_array_equal(result, expected)
300300

301301
def test_take_coerces_list(self):
302+
# GH#52981 coercing is deprecated, disabled in 3.0
302303
arr = [1, 2, 3]
303-
msg = "take accepting non-standard inputs is deprecated"
304-
with tm.assert_produces_warning(FutureWarning, match=msg):
305-
result = algos.take(arr, [0, 0])
306-
expected = np.array([1, 1])
307-
tm.assert_numpy_array_equal(result, expected)
304+
msg = (
305+
"pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, "
306+
"Index, or Series, got list"
307+
)
308+
with pytest.raises(TypeError, match=msg):
309+
algos.take(arr, [0, 0])

0 commit comments

Comments
 (0)