Skip to content

Add support for NumpyExtensionArray in pd.unique() #59214

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 2 commits into from
Jul 9, 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
19 changes: 15 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,17 @@ def _ensure_arraylike(values, func_name: str) -> ArrayLike:
"""
ensure that we are arraylike if not already
"""
if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)):
if not isinstance(
values,
(ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray, ABCNumpyExtensionArray),
):
# GH#52986
if func_name != "isin-targets":
# Make an exception for the comps argument in isin.
raise TypeError(
f"{func_name} requires a Series, Index, "
f"ExtensionArray, or np.ndarray, got {type(values).__name__}."
f"ExtensionArray, np.ndarray or NumpyExtensionArray "
f"got {type(values).__name__}."
)

inferred = lib.infer_dtype(values, skipna=False)
Expand Down Expand Up @@ -325,15 +329,15 @@ def unique(values):

Returns
-------
numpy.ndarray or ExtensionArray
numpy.ndarray, ExtensionArray or NumpyExtensionArray

The return can be:

* Index : when the input is an Index
* Categorical : when the input is a Categorical dtype
* ndarray : when the input is a Series/ndarray

Return numpy.ndarray or ExtensionArray.
Return numpy.ndarray, ExtensionArray or NumpyExtensionArray.

See Also
--------
Expand Down Expand Up @@ -405,6 +409,13 @@ def unique(values):

>>> pd.unique(pd.Series([("a", "b"), ("b", "a"), ("a", "c"), ("b", "a")]).values)
array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)

An NumpyExtensionArray of complex

>>> pd.unique(pd.array([1 + 1j, 2, 3]))
<NumpyExtensionArray>
[(1+1j), (2+0j), (3+0j)]
Length: 3, dtype: complex128
"""
return unique_with_mask(values)

Expand Down
19 changes: 17 additions & 2 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,14 @@ def test_unique_masked(self, any_numeric_ea_dtype):
expected = pd.array([1, pd.NA, 2], dtype=any_numeric_ea_dtype)
tm.assert_extension_array_equal(result, expected)

def test_unique_NumpyExtensionArray(self):
arr_complex = pd.array(
[1 + 1j, 2, 3]
) # NumpyEADtype('complex128') => NumpyExtensionArray
result = pd.unique(arr_complex)
expected = pd.array([1 + 1j, 2 + 0j, 3 + 0j])
tm.assert_extension_array_equal(result, expected)


def test_nunique_ints(index_or_series_or_array):
# GH#36327
Expand Down Expand Up @@ -1638,7 +1646,10 @@ def test_unique_tuples(self, arr, uniques):
expected = np.empty(len(uniques), dtype=object)
expected[:] = uniques

msg = "unique requires a Series, Index, ExtensionArray, or np.ndarray, got list"
msg = (
r"unique requires a Series, Index, ExtensionArray, np.ndarray "
r"or NumpyExtensionArray got list"
)
with pytest.raises(TypeError, match=msg):
# GH#52986
pd.unique(arr)
Expand All @@ -1657,7 +1668,11 @@ def test_unique_tuples(self, arr, uniques):
)
def test_unique_complex_numbers(self, array, expected):
# GH 17927
msg = "unique requires a Series, Index, ExtensionArray, or np.ndarray, got list"
msg = (
r"unique requires a Series, Index, ExtensionArray, np.ndarray "
r"or NumpyExtensionArray got list"
)

with pytest.raises(TypeError, match=msg):
# GH#52986
pd.unique(array)
Expand Down