Skip to content

BUG: pd.unique(Index) now returns Index as Index.unique #57679

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 11 commits into from
Mar 12, 2024
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ Other
^^^^^
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
- Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`)
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ def unique_with_mask(values, mask: npt.NDArray[np.bool_] | None = None):
# Dispatch to extension dtype's unique.
return values.unique()

if isinstance(values, ABCIndex):
# Dispatch to Index's unique.
return values.unique()

original = values
hashtable, values = _get_hashtable_algo(values)

Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ def test_object_refcount_bug(self):
for i in range(1000):
len(algos.unique(lst))

def test_index_returned(self, index):
# GH#57043
index = index.repeat(2)
result = algos.unique(index)
expected = index.unique()
Copy link
Member

Choose a reason for hiding this comment

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

Is it doable to specify expected explicitly here instead of using index.unique()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can adapt the test from

def test_unique(index_or_series_obj):

tm.assert_index_equal(result, expected, exact=True)

def test_on_index_object(self):
mindex = MultiIndex.from_arrays(
[np.arange(5).repeat(5), np.tile(np.arange(5), 5)]
Expand All @@ -579,7 +586,7 @@ def test_on_index_object(self):

mindex = mindex.repeat(2)

result = pd.unique(mindex)
result = pd.unique(mindex).values
result.sort()

tm.assert_almost_equal(result, expected)
Expand Down Expand Up @@ -639,7 +646,7 @@ def test_datetime64_dtype_array_returned(self):
]
)
result = algos.unique(dt_index)
tm.assert_numpy_array_equal(result, expected)
tm.assert_index_equal(result, to_datetime(expected))
assert result.dtype == expected.dtype

s = Series(dt_index)
Expand Down Expand Up @@ -670,7 +677,7 @@ def test_timedelta64_dtype_array_returned(self):

td_index = to_timedelta([31200, 45678, 31200, 10000, 45678])
result = algos.unique(td_index)
tm.assert_numpy_array_equal(result, expected)
tm.assert_index_equal(result, to_timedelta(expected))
assert result.dtype == expected.dtype

s = Series(td_index)
Expand Down