Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,16 +2081,9 @@ def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
if self.dtype == values.dtype:
# GH#38353 instead of casting to object, operating on a
# complex128 ndarray is much more performant.
left = self._combined.view("complex128")
right = values._combined.view("complex128")
# error: Argument 1 to "isin" has incompatible type
# "Union[ExtensionArray, ndarray[Any, Any],
# ndarray[Any, dtype[Any]]]"; expected
# "Union[_SupportsArray[dtype[Any]],
# _NestedSequence[_SupportsArray[dtype[Any]]], bool,
# int, float, complex, str, bytes, _NestedSequence[
# Union[bool, int, float, complex, str, bytes]]]"
return np.isin(left, right).ravel() # type: ignore[arg-type]
left = self._combined
right = values._combined
return np.isin(left, right).ravel()
Comment on lines 2082 to +2086
Copy link
Member

@rhshadrach rhshadrach Oct 31, 2025

Choose a reason for hiding this comment

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

I think the comment about complex128 is no longer relevant?


elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
values.left.dtype
Expand All @@ -2112,18 +2105,21 @@ def _combined(self) -> IntervalSide:
comb = left._concat_same_type( # type: ignore[union-attr]
[left, right], axis=1
)
comb = comb.view("complex128")[:, 0]
else:
comb = np.concatenate([left, right], axis=1)
comb = (np.array(left.ravel(), dtype="complex128")) + (
1j * np.array(right.ravel(), dtype="complex128")
)
Comment on lines +2108 to +2112
Copy link
Member

Choose a reason for hiding this comment

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

Should the comment from above be moved down here in some form?

return comb

def _from_combined(self, combined: np.ndarray) -> IntervalArray:
"""
Create a new IntervalArray with our dtype from a 1D complex128 ndarray.
"""
nc = combined.view("i8").reshape(-1, 2)

dtype = self._left.dtype
if needs_i8_conversion(dtype):
nc = combined.view("i8").reshape(-1, 2)
assert isinstance(self._left, (DatetimeArray, TimedeltaArray))
new_left: DatetimeArray | TimedeltaArray | np.ndarray = type(
self._left
Expand All @@ -2134,18 +2130,13 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
)._from_sequence(nc[:, 1], dtype=dtype)
else:
assert isinstance(dtype, np.dtype)
new_left = nc[:, 0].view(dtype)
new_right = nc[:, 1].view(dtype)
new_left = np.real(combined).astype(dtype).ravel()
new_right = np.imag(combined).astype(dtype).ravel()
return self._shallow_copy(left=new_left, right=new_right)

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]
)
nc = nc[:, None]
return self._from_combined(nc)
nc = unique(self._combined)
return self._from_combined(np.asarray(nc)[:, None])


def _maybe_convert_platform_interval(values) -> ArrayLike:
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ 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()
expected = IntervalIndex.from_tuples([(3, 4), (2, 3), (1, 2)])
tm.assert_index_equal(result, expected)

idx_neg = IntervalIndex.from_tuples(
[(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)]
)
result = idx_neg.unique()
expected = IntervalIndex.from_tuples([(-4, -3), (-3, -2), (-2, -1)])
tm.assert_index_equal(result, expected)

idx_mix = IntervalIndex.from_tuples(
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2), (-3, -2)]
)
result = idx_mix.unique()
expected = IntervalIndex.from_tuples(
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2)]
)
tm.assert_index_equal(result, expected)


class TestSetitem:
def test_set_na(self, left_right_dtypes):
Expand Down
Loading