Skip to content

Commit 9be72c7

Browse files
KhemkaranKhemkaran
authored andcommitted
make changes to unique() in interval.py
1 parent 3547d84 commit 9be72c7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pandas/core/arrays/interval.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,10 +1971,10 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
19711971
"""
19721972
Create a new IntervalArray with our dtype from a 1D complex128 ndarray.
19731973
"""
1974-
nc = combined.view("i8").reshape(-1, 2)
19751974

19761975
dtype = self._left.dtype
19771976
if needs_i8_conversion(dtype):
1977+
nc = combined.view("i8").reshape(-1, 2)
19781978
assert isinstance(self._left, (DatetimeArray, TimedeltaArray))
19791979
new_left: DatetimeArray | TimedeltaArray | np.ndarray = type(
19801980
self._left
@@ -1985,6 +1985,9 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
19851985
)._from_sequence(nc[:, 1], dtype=dtype)
19861986
else:
19871987
assert isinstance(dtype, np.dtype)
1988+
nc = np.hstack(
1989+
[np.real(combined).astype(dtype), np.imag(combined).astype(dtype)]
1990+
).reshape(-1, 2)
19881991
new_left = nc[:, 0].view(dtype)
19891992
new_right = nc[:, 1].view(dtype)
19901993
return self._shallow_copy(left=new_left, right=new_right)

pandas/tests/arrays/interval/test_interval.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,24 @@ def test_unique_with_negatives(self):
117117
[(3, 4), (3, 4), (2, 3), (2, 3), (1, 2), (1, 2)]
118118
)
119119
result = idx_pos.unique()
120-
assert result.shape == (3,), f"Expected shape (3,), got {result.shape}"
120+
expected = IntervalIndex.from_tuples([(3, 4), (2, 3), (1, 2)])
121+
tm.assert_index_equal(result, expected)
121122

122123
idx_neg = IntervalIndex.from_tuples(
123124
[(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)]
124125
)
125126
result = idx_neg.unique()
126-
assert result.shape == (3,), f"Expected shape (3,), got {result.shape}"
127+
expected = IntervalIndex.from_tuples([(-4, -3), (-3, -2), (-2, -1)])
128+
tm.assert_index_equal(result, expected)
127129

128130
idx_mix = IntervalIndex.from_tuples(
129131
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2), (-3, -2)]
130132
)
131133
result = idx_mix.unique()
132-
assert result.shape == (5,), f"Expected shape (5,), got {result.shape}"
134+
expected = IntervalIndex.from_tuples(
135+
[(1, 2), (0, 1), (-1, 0), (-2, -1), (-3, -2)]
136+
)
137+
tm.assert_index_equal(result, expected)
133138

134139

135140
class TestSetitem:

0 commit comments

Comments
 (0)