diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 4bcbe2eedee47..10a8313851396 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1992,9 +1992,17 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray: def unique(self) -> IntervalArray: # No overload variant of "__getitem__" of "ExtensionArray" matches argument # type "Tuple[slice, int]" - nc = unique( - self._combined.view("complex128")[:, 0] # type: ignore[call-overload] - ) + if needs_i8_conversion(self._left.dtype): + nc = unique( + self._combined.view("complex128")[:, 0] # type: ignore[call-overload] + ) + else: + nc = unique( + # Using .view("complex128") with negatives causes issues. + # GH#61917 + (np.array(self._combined[:, 0], dtype=complex)) + + (1j * np.array(self._combined[:, 1], dtype=complex)) + ) nc = nc[:, None] return self._from_combined(nc) diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 8e13dcf25ceba..2cdeb9ae1701c 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -111,6 +111,26 @@ def test_shift_datetime(self): with pytest.raises(TypeError, match=msg): a.shift(1, fill_value=np.timedelta64("NaT", "ns")) + def test_unique_with_negatives(self): + # GH#61917 + idx_pos = IntervalIndex.from_tuples( + [(3, 4), (3, 4), (2, 3), (2, 3), (1, 2), (1, 2)] + ) + result = idx_pos.unique() + assert result.shape == (3,), f"Expected shape (3,), got {result.shape}" + + idx_neg = IntervalIndex.from_tuples( + [(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)] + ) + result = idx_neg.unique() + assert result.shape == (3,), f"Expected shape (3,), got {result.shape}" + + idx_mix = IntervalIndex.from_tuples( + [(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2), (-3, -2)] + ) + result = idx_mix.unique() + assert result.shape == (5,), f"Expected shape (5,), got {result.shape}" + class TestSetitem: def test_set_na(self, left_right_dtypes):