Skip to content

BUG: IntervalIndex.unique() only contains the first interval if all interval borders are negative #61920

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,10 +1971,10 @@ 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 @@ -1985,6 +1985,9 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
)._from_sequence(nc[:, 1], dtype=dtype)
else:
assert isinstance(dtype, np.dtype)
nc = np.hstack(
Copy link
Member

Choose a reason for hiding this comment

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

is the hstack really necessary here since the next 2 lines is just splitting them back up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @jbrockmendel , It's not required. I'll change this.

[np.real(combined).astype(dtype), np.imag(combined).astype(dtype)]
).reshape(-1, 2)
new_left = nc[:, 0].view(dtype)
new_right = nc[:, 1].view(dtype)
return self._shallow_copy(left=new_left, right=new_right)
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,24 @@ def test_unique_with_negatives(self):
[(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}"
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()
assert result.shape == (3,), f"Expected shape (3,), got {result.shape}"
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()
assert result.shape == (5,), f"Expected shape (5,), got {result.shape}"
expected = IntervalIndex.from_tuples(
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2)]
)
tm.assert_index_equal(result, expected)


class TestSetitem:
Expand Down
Loading