Skip to content

BUG: pd.unique() does not accept NumpyExtensionArray #59213

Closed
@mutricyl

Description

@mutricyl

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
arr_int = pd.array([1, 2, 3]) # Int64Dtype() => ExtensionArray
arr_complex = pd.array([1+1j, 2, 3]) # NumpyEADtype('complex128') => NumpyExtensionArray

pd.unique(arr_int)  # OK
pd.unique(arr_complex)  # NOK: TypeError: unique requires a Series, Index, ExtensionArray, or np.ndarray, got NumpyExtensionArray.

Issue Description

This issue is similar to #59177 and is coming from pint-pandas where test CI job with nightly builds from pandas was recently introduced (pint-pandas issue related to skipped tests to pass tests with pandas 3).

The above example is fine with pandas 2.2.2 but fails with pandas nightlies

The above mentionned TypeError is coming from

def _ensure_arraylike(values, func_name: str) -> ArrayLike:

As explained in #59177 NumpyExtensionArray may be used internally when other type of ExtensionArray are not available.

Expected Behavior

for pandas 2.2.2 pd.unique returns:

>>> pd.unique(arr_complex)
array([1.+1.j, 2.+0.j, 3.+0.j])

While we expect

>>> pd.unique(arr_complex)
<NumpyExtensionArray>
[(1+1j), (2+0j), (3+0j)]
Length: 3, dtype: complex128

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

Labels

BugNeeds TriageIssue that has not been reviewed by a pandas team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions