Skip to content

DEPR: allowing unknowns in take #52981

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 3 commits into from
May 30, 2023
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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)