diff --git a/pandas/_libs/interval.pyi b/pandas/_libs/interval.pyi index 67cb604083c6b..bad6013aa58b6 100644 --- a/pandas/_libs/interval.pyi +++ b/pandas/_libs/interval.pyi @@ -32,8 +32,6 @@ class _LengthDescriptor: def __get__( self, instance: Interval[_OrderableTimesT], owner: Any ) -> Timedelta: ... - @overload - def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ... class _MidDescriptor: @overload @@ -42,8 +40,6 @@ class _MidDescriptor: def __get__( self, instance: Interval[_OrderableTimesT], owner: Any ) -> _OrderableTimesT: ... - @overload - def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ... class IntervalMixin: @property @@ -54,8 +50,6 @@ class IntervalMixin: def open_left(self) -> bool: ... @property def open_right(self) -> bool: ... - mid: _MidDescriptor - length: _LengthDescriptor @property def is_empty(self) -> bool: ... def _check_closed_matches(self, other: IntervalMixin, name: str = ...) -> None: ... @@ -67,6 +61,8 @@ class Interval(IntervalMixin, Generic[_OrderableT]): def right(self: Interval[_OrderableT]) -> _OrderableT: ... @property def closed(self) -> IntervalClosedType: ... + mid: _MidDescriptor + length: _LengthDescriptor def __init__( self, left: _OrderableT, @@ -162,6 +158,10 @@ class IntervalTree(IntervalMixin): closed: IntervalClosedType = ..., leaf_size: int = ..., ): ... + @property + def mid(self) -> np.ndarray: ... + @property + def length(self) -> np.ndarray: ... def get_indexer(self, target) -> npt.NDArray[np.intp]: ... def get_indexer_non_unique( self, target diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 63f8fc0262199..e14eec419377c 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -7,6 +7,7 @@ ) import textwrap from typing import ( + TYPE_CHECKING, Sequence, TypeVar, Union, @@ -93,6 +94,10 @@ unpack_zerodim_and_defer, ) +if TYPE_CHECKING: + from pandas import Index + + IntervalArrayT = TypeVar("IntervalArrayT", bound="IntervalArray") IntervalOrNA = Union[Interval, float] @@ -1230,7 +1235,7 @@ def right(self): return Index(self._right, copy=False) @property - def length(self): + def length(self) -> Index: """ Return an Index with entries denoting the length of each Interval in the IntervalArray. @@ -1238,7 +1243,7 @@ def length(self): return self.right - self.left @property - def mid(self): + def mid(self) -> Index: """ Return the midpoint of each Interval in the IntervalArray as an Index. """