Skip to content

TYP: fix mid and length for Interval and Intervalarray #46472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pandas/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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: ...
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
import textwrap
from typing import (
TYPE_CHECKING,
Sequence,
TypeVar,
Union,
Expand Down Expand Up @@ -93,6 +94,10 @@
unpack_zerodim_and_defer,
)

if TYPE_CHECKING:
from pandas import Index


IntervalArrayT = TypeVar("IntervalArrayT", bound="IntervalArray")
IntervalOrNA = Union[Interval, float]

Expand Down Expand Up @@ -1230,15 +1235,15 @@ 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.
"""
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.
"""
Expand Down