Skip to content

Commit deea1a0

Browse files
KhemkaranKhemkaran
authored andcommitted
Fix issue 61917, modified uniqe() func in interval.py
1 parent 13f7b8b commit deea1a0

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

pandas/core/arrays/interval.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,12 +1990,15 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
19901990
return self._shallow_copy(left=new_left, right=new_right)
19911991

19921992
def unique(self) -> IntervalArray:
1993-
# No overload variant of "__getitem__" of "ExtensionArray" matches argument
1994-
# type "Tuple[slice, int]"
1995-
nc = unique(
1996-
self._combined.view("complex128")[:, 0] # type: ignore[call-overload]
1997-
)
1998-
nc = nc[:, None]
1993+
# Using .view("complex128") with negatives causes issues. # GH#61917
1994+
combined = self._combined
1995+
if not combined.flags.c_contiguous:
1996+
combined = np.ascontiguousarray(combined)
1997+
structured = combined.view(
1998+
[("left", combined.dtype), ("right", combined.dtype)]
1999+
)[:, 0]
2000+
unique_structured = unique(structured)
2001+
nc = unique_structured.view(combined.dtype)
19992002
return self._from_combined(nc)
20002003

20012004

pandas/tests/arrays/interval/test_interval.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ def test_shift_datetime(self):
111111
with pytest.raises(TypeError, match=msg):
112112
a.shift(1, fill_value=np.timedelta64("NaT", "ns"))
113113

114+
def test_unique_with_negatives(self):
115+
# GH#61917
116+
idx_pos = IntervalIndex.from_tuples(
117+
[(3, 4), (3, 4), (2, 3), (2, 3), (1, 2), (1, 2)]
118+
)
119+
result = idx_pos.unique()
120+
assert result.shape == (3,), f"Expected shape (3,), got {result.shape}"
121+
122+
idx_neg = IntervalIndex.from_tuples(
123+
[(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)]
124+
)
125+
result = idx_neg.unique()
126+
assert result.shape == (3,), f"Expected shape (3,), got {result.shape}"
127+
128+
idx_mix = IntervalIndex.from_tuples(
129+
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2), (-3, -2)]
130+
)
131+
result = idx_mix.unique()
132+
assert result.shape == (5,), f"Expected shape (5,), got {result.shape}"
133+
114134

115135
class TestSetitem:
116136
def test_set_na(self, left_right_dtypes):

0 commit comments

Comments
 (0)