Repository: pandas-dev/pandas
File: pandas/core/arrays/interval.py (Python)

Code before:

            elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
                values.left.dtype
            ):
                # not comparable -> no overlap
                return np.zeros(self.shape, dtype=bool)

        return isin(self.astype(object), values.astype(object))

    @property
    def _combined(self) -> IntervalSide:
        # error: Item "ExtensionArray" of "ExtensionArray | ndarray[Any, Any]"
        # has no attribute "reshape"  [union-attr]
        left = self.left._values.reshape(-1, 1)  # type: ignore[union-attr]
        right = self.right._values.reshape(-1, 1)  # type: ignore[union-attr]
        # GH#38353 instead of casting to object, operating on a
        # complex128 ndarray is much more performant.
        if needs_i8_conversion(left.dtype):
            # error: Item "ndarray[Any, Any]" of "Any | ndarray[Any, Any]" has
            # no attribute "_concat_same_type"
            comb = left._concat_same_type(  # type: ignore[union-attr]
                [left, right], axis=1
            )
            comb = comb.view("complex128")[:, 0]
        else:
            comb = np.array(left.ravel(), dtype="complex128")
            comb.imag = right.ravel()
        return comb

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

        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
            )._from_sequence(nc[:, 0], dtype=dtype)
            assert isinstance(self._right, (DatetimeArray, TimedeltaArray))
            new_right: DatetimeArray | TimedeltaArray | np.ndarray = type(
                self._right
            )._from_sequence(nc[:, 1], dtype=dtype)
        else:
            assert isinstance(dtype, np.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)


Review comment (suggestion):
Could we use `np.asarray`? (NumPy might copy anyways but would be nice if it didn't in this case)

Code after:

            elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
                values.left.dtype
            ):
                # not comparable -> no overlap
                return np.zeros(self.shape, dtype=bool)

        return isin(self.astype(object), values.astype(object))

    @property
    def _combined(self) -> IntervalSide:
        # error: Item "ExtensionArray" of "ExtensionArray | ndarray[Any, Any]"
        # has no attribute "reshape"  [union-attr]
        left = self.left._values.reshape(-1, 1)  # type: ignore[union-attr]
        right = self.right._values.reshape(-1, 1)  # type: ignore[union-attr]
        # GH#38353 instead of casting to object, operating on a
        # complex128 ndarray is much more performant.
        if needs_i8_conversion(left.dtype):
            # error: Item "ndarray[Any, Any]" of "Any | ndarray[Any, Any]" has
            # no attribute "_concat_same_type"
            comb = left._concat_same_type(  # type: ignore[union-attr]
                [left, right], axis=1
            )
            comb = comb.view("complex128")[:, 0]
        else:
            comb = np.asarray(left.ravel(), dtype="complex128")
            comb.imag = right.ravel()
        return comb

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

        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
            )._from_sequence(nc[:, 0], dtype=dtype)
            assert isinstance(self._right, (DatetimeArray, TimedeltaArray))
            new_right: DatetimeArray | TimedeltaArray | np.ndarray = type(
                self._right
            )._from_sequence(nc[:, 1], dtype=dtype)
        else:
            assert isinstance(dtype, np.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)