Exemple #1
0
def _get_combined_data(
    left: Union["Index", ArrayLike], right: Union["Index", ArrayLike]
) -> Union[np.ndarray, "DatetimeArray", "TimedeltaArray"]:
    # For dt64/td64 we want DatetimeArray/TimedeltaArray instead of ndarray
    from pandas.core.ops.array_ops import maybe_upcast_datetimelike_array

    left = maybe_upcast_datetimelike_array(left)
    left = extract_array(left, extract_numpy=True)
    right = maybe_upcast_datetimelike_array(right)
    right = extract_array(right, extract_numpy=True)

    lbase = getattr(left, "_ndarray", left).base
    rbase = getattr(right, "_ndarray", right).base
    if lbase is not None and lbase is rbase:
        # If these share data, then setitem could corrupt our IA
        right = right.copy()

    if isinstance(left, np.ndarray):
        assert isinstance(right, np.ndarray)  # for mypy
        combined = np.concatenate(
            [left.reshape(-1, 1), right.reshape(-1, 1)],
            axis=1,
        )
    else:
        left = cast(Union["DatetimeArray", "TimedeltaArray"], left)
        right = cast(Union["DatetimeArray", "TimedeltaArray"], right)
        combined = type(left)._concat_same_type(
            [left.reshape(-1, 1), right.reshape(-1, 1)],
            axis=1,
        )
    return combined
Exemple #2
0
def _get_combined_data(
    left: Union["Index", ArrayLike], right: Union["Index", ArrayLike]
) -> Union[np.ndarray, "DatetimeArray", "TimedeltaArray"]:
    # For dt64/td64 we want DatetimeArray/TimedeltaArray instead of ndarray
    from pandas.core.ops.array_ops import maybe_upcast_datetimelike_array

    left = maybe_upcast_datetimelike_array(left)
    left = extract_array(left, extract_numpy=True)
    right = maybe_upcast_datetimelike_array(right)
    right = extract_array(right, extract_numpy=True)

    lbase = getattr(left, "_ndarray", left).base
    rbase = getattr(right, "_ndarray", right).base
    if lbase is not None and lbase is rbase:
        # If these share data, then setitem could corrupt our IA
        right = right.copy()

    if isinstance(left, np.ndarray):
        assert isinstance(right, np.ndarray)  # for mypy
        combined = np.concatenate(
            [left.reshape(-1, 1), right.reshape(-1, 1)],
            axis=1,
        )
    else:
        # error: Item "type" of "Union[Type[Index], Type[ExtensionArray]]" has
        # no attribute "_concat_same_type"  [union-attr]

        # error: Unexpected keyword argument "axis" for "_concat_same_type" of
        # "ExtensionArray" [call-arg]

        # error: Item "Index" of "Union[Index, ExtensionArray]" has no
        # attribute "reshape" [union-attr]

        # error: Item "ExtensionArray" of "Union[Index, ExtensionArray]" has no
        # attribute "reshape" [union-attr]
        combined = type(
            left)._concat_same_type(  # type: ignore[union-attr,call-arg]
                [left.reshape(-1, 1),
                 right.reshape(-1, 1)],  # type: ignore[union-attr]
                axis=1,
            )
    return combined
Exemple #3
0
    def _simple_new(
        cls, left, right, closed=None, copy=False, dtype=None, verify_integrity=True
    ):
        result = IntervalMixin.__new__(cls)

        closed = closed or "right"
        left = ensure_index(left, copy=copy)
        right = ensure_index(right, copy=copy)

        if dtype is not None:
            # GH 19262: dtype must be an IntervalDtype to override inferred
            dtype = pandas_dtype(dtype)
            if not is_interval_dtype(dtype):
                msg = f"dtype must be an IntervalDtype, got {dtype}"
                raise TypeError(msg)
            elif dtype.subtype is not None:
                left = left.astype(dtype.subtype)
                right = right.astype(dtype.subtype)

        # coerce dtypes to match if needed
        if is_float_dtype(left) and is_integer_dtype(right):
            right = right.astype(left.dtype)
        elif is_float_dtype(right) and is_integer_dtype(left):
            left = left.astype(right.dtype)

        if type(left) != type(right):
            msg = (
                f"must not have differing left [{type(left).__name__}] and "
                f"right [{type(right).__name__}] types"
            )
            raise ValueError(msg)
        elif is_categorical_dtype(left.dtype) or is_string_dtype(left.dtype):
            # GH 19016
            msg = (
                "category, object, and string subtypes are not supported "
                "for IntervalArray"
            )
            raise TypeError(msg)
        elif isinstance(left, ABCPeriodIndex):
            msg = "Period dtypes are not supported, use a PeriodIndex instead"
            raise ValueError(msg)
        elif isinstance(left, ABCDatetimeIndex) and str(left.tz) != str(right.tz):
            msg = (
                "left and right must have the same time zone, got "
                f"'{left.tz}' and '{right.tz}'"
            )
            raise ValueError(msg)

        # For dt64/td64 we want DatetimeArray/TimedeltaArray instead of ndarray
        from pandas.core.ops.array_ops import maybe_upcast_datetimelike_array

        left = maybe_upcast_datetimelike_array(left)
        left = extract_array(left, extract_numpy=True)
        right = maybe_upcast_datetimelike_array(right)
        right = extract_array(right, extract_numpy=True)

        lbase = getattr(left, "_ndarray", left).base
        rbase = getattr(right, "_ndarray", right).base
        if lbase is not None and lbase is rbase:
            # If these share data, then setitem could corrupt our IA
            right = right.copy()

        result._left = left
        result._right = right
        result._closed = closed
        if verify_integrity:
            result._validate()
        return result