Skip to content

BUG: Fix is_unique regression for slices of Indexes #57958

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 13 commits into from
Aug 6, 2024
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pandas nullable on with missing values (:issue:`56702`)
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pyarrow nullable on with missing values (:issue:`57664`)
- :meth:`Index.is_unique` could incorrectly return false if the ``Index`` was created from a slice of another ``Index``. (:issue:`57911`)
-

.. ---------------------------------------------------------------------------
Expand Down
12 changes: 8 additions & 4 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ cdef class IndexEngine:
return self.sizeof()

cpdef _update_from_sliced(self, IndexEngine other, reverse: bool):
self.unique = other.unique
self.need_unique_check = other.need_unique_check
if other.unique:
self.unique = 1
self.need_unique_check = 0

if not other.need_monotonic_check and (
other.is_monotonic_increasing or other.is_monotonic_decreasing):
self.need_monotonic_check = other.need_monotonic_check
Expand Down Expand Up @@ -854,8 +856,10 @@ cdef class SharedEngine:
pass

cpdef _update_from_sliced(self, ExtensionEngine other, reverse: bool):
self.unique = other.unique
self.need_unique_check = other.need_unique_check
if other.unique:
self.unique = 1
self.need_unique_check = 0

if not other.need_monotonic_check and (
other.is_monotonic_increasing or other.is_monotonic_decreasing):
self.need_monotonic_check = other.need_monotonic_check
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,13 @@ def test_slice_keep_name(self):
index = Index(["a", "b"], name="asdf")
assert index.name == index[1:].name

def test_slice_is_unique(self):
# GH 57911
index = Index([1, 1, 2, 3, 4])
assert not index.is_unique
filtered_index = index[2:].copy()
assert filtered_index.is_unique

@pytest.mark.parametrize(
"index",
[
Expand Down