Skip to content

DEPR: enforce deprecation of non-standard argument to take #58011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
25 changes: 13 additions & 12 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
ensure_float64,
ensure_object,
ensure_platform_int,
is_array_like,
is_bool_dtype,
is_complex_dtype,
is_dict_like,
Expand Down Expand Up @@ -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


Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])