Skip to content

Commit fce684f

Browse files
KhemkaranKhemkaran
authored andcommitted
added complex array creation logic in _combined directly
1 parent 9be72c7 commit fce684f

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

pandas/core/arrays/interval.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,16 +1932,16 @@ def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
19321932
if self.dtype == values.dtype:
19331933
# GH#38353 instead of casting to object, operating on a
19341934
# complex128 ndarray is much more performant.
1935-
left = self._combined.view("complex128")
1936-
right = values._combined.view("complex128")
1935+
left = self._combined
1936+
right = values._combined
19371937
# error: Argument 1 to "isin" has incompatible type
19381938
# "Union[ExtensionArray, ndarray[Any, Any],
19391939
# ndarray[Any, dtype[Any]]]"; expected
19401940
# "Union[_SupportsArray[dtype[Any]],
19411941
# _NestedSequence[_SupportsArray[dtype[Any]]], bool,
19421942
# int, float, complex, str, bytes, _NestedSequence[
19431943
# Union[bool, int, float, complex, str, bytes]]]"
1944-
return np.isin(left, right).ravel() # type: ignore[arg-type]
1944+
return np.isin(left, right).ravel()
19451945

19461946
elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
19471947
values.left.dtype
@@ -1963,8 +1963,11 @@ def _combined(self) -> IntervalSide:
19631963
comb = left._concat_same_type( # type: ignore[union-attr]
19641964
[left, right], axis=1
19651965
)
1966+
comb = comb.view("complex128")[:, 0]
19661967
else:
1967-
comb = np.concatenate([left, right], axis=1)
1968+
comb = (np.array(left.ravel(), dtype=complex)) + (
1969+
1j * np.array(right.ravel(), dtype=complex)
1970+
)
19681971
return comb
19691972

19701973
def _from_combined(self, combined: np.ndarray) -> IntervalArray:
@@ -1985,27 +1988,14 @@ def _from_combined(self, combined: np.ndarray) -> IntervalArray:
19851988
)._from_sequence(nc[:, 1], dtype=dtype)
19861989
else:
19871990
assert isinstance(dtype, np.dtype)
1988-
nc = np.hstack(
1989-
[np.real(combined).astype(dtype), np.imag(combined).astype(dtype)]
1990-
).reshape(-1, 2)
1991-
new_left = nc[:, 0].view(dtype)
1992-
new_right = nc[:, 1].view(dtype)
1991+
new_left = np.real(combined).astype(dtype).ravel()
1992+
new_right = np.imag(combined).astype(dtype).ravel()
19931993
return self._shallow_copy(left=new_left, right=new_right)
19941994

19951995
def unique(self) -> IntervalArray:
19961996
# No overload variant of "__getitem__" of "ExtensionArray" matches argument
19971997
# type "Tuple[slice, int]"
1998-
if needs_i8_conversion(self._left.dtype):
1999-
nc = unique(
2000-
self._combined.view("complex128")[:, 0] # type: ignore[call-overload]
2001-
)
2002-
else:
2003-
nc = unique(
2004-
# Using .view("complex128") with negatives causes issues.
2005-
# GH#61917
2006-
(np.array(self._combined[:, 0], dtype=complex))
2007-
+ (1j * np.array(self._combined[:, 1], dtype=complex))
2008-
)
1998+
nc = unique(self._combined)
20091999
nc = nc[:, None]
20102000
return self._from_combined(nc)
20112001

0 commit comments

Comments
 (0)