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 3 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
8 changes: 7 additions & 1 deletion pandas/core/dtypes/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ def _subclasscheck(cls, inst) -> bool:
"ABCExtensionArray",
"_typ",
# Note: IntervalArray and SparseArray are included bc they have _typ="extension"
{"extension", "categorical", "periodarray", "datetimearray", "timedeltaarray"},
{
"extension",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct in seeing that just for formatting here changed? i.e. There is no new elements added to this set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

short story

Indeed the set content have not changed.

long story

I first tried to resolve the issue adding one element to the set which induces black to change formatting. I realized afterwards that ABCNumpyExtensionArray already existed so I finaly used it. I forgot at the end to revert the formating change for ABCExtensionArray

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, could you undo the formatting here?

"categorical",
"periodarray",
"datetimearray",
"timedeltaarray",
},
),
)
ABCNumpyExtensionArray = cast(
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