Skip to content

update algo.take to solve #59177 #59181

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 4 commits into from
Jul 6, 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
10 changes: 7 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
ABCExtensionArray,
ABCIndex,
ABCMultiIndex,
ABCNumpyExtensionArray,
ABCSeries,
ABCTimedeltaArray,
)
Expand Down Expand Up @@ -1161,11 +1162,14 @@ def take(
... )
array([ 10, 10, -10])
"""
if not isinstance(arr, (np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries)):
if not isinstance(
arr,
(np.ndarray, ABCExtensionArray, ABCIndex, ABCSeries, ABCNumpyExtensionArray),
):
# GH#52981
raise TypeError(
"pd.api.extensions.take requires a numpy.ndarray, "
f"ExtensionArray, Index, or Series, got {type(arr).__name__}."
"pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, "
f"Index, Series, or NumpyExtensionArray got {type(arr).__name__}."
)

indices = ensure_platform_int(indices)
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/test_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pandas._libs import iNaT

from pandas import array
import pandas._testing as tm
import pandas.core.algorithms as algos

Expand Down Expand Up @@ -303,7 +304,14 @@ def test_take_coerces_list(self):
arr = [1, 2, 3]
msg = (
"pd.api.extensions.take requires a numpy.ndarray, ExtensionArray, "
"Index, or Series, got list"
"Index, Series, or NumpyExtensionArray got list"
)
with pytest.raises(TypeError, match=msg):
algos.take(arr, [0, 0])

def test_take_NumpyExtensionArray(self):
# GH#59177
arr = array([1 + 1j, 2, 3]) # NumpyEADtype('complex128') (NumpyExtensionArray)
assert algos.take(arr, [2]) == 2
arr = array([1, 2, 3]) # Int64Dtype() (ExtensionArray)
assert algos.take(arr, [2]) == 2
Loading