Skip to content

Commit 66a5de3

Browse files
authored
Typeinterval part2 (#46098)
1 parent 661d88e commit 66a5de3

File tree

3 files changed

+183
-9
lines changed

3 files changed

+183
-9
lines changed

pandas/_libs/interval.pyi

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
from __future__ import annotations
2+
3+
from typing import (
4+
Any,
5+
Generic,
6+
TypeVar,
7+
Union,
8+
overload,
9+
)
10+
11+
import numpy as np
12+
import numpy.typing as npt
13+
14+
from pandas._typing import (
15+
IntervalClosedType,
16+
Timedelta,
17+
Timestamp,
18+
)
19+
20+
VALID_CLOSED: frozenset[str]
21+
22+
_OrderableScalarT = TypeVar("_OrderableScalarT", int, float)
23+
_OrderableTimesT = TypeVar("_OrderableTimesT", Timestamp, Timedelta)
24+
_OrderableT = TypeVar("_OrderableT", int, float, Timestamp, Timedelta)
25+
26+
class _LengthDescriptor:
27+
@overload
28+
def __get__(
29+
self, instance: Interval[_OrderableScalarT], owner: Any
30+
) -> _OrderableScalarT: ...
31+
@overload
32+
def __get__(
33+
self, instance: Interval[_OrderableTimesT], owner: Any
34+
) -> Timedelta: ...
35+
@overload
36+
def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ...
37+
38+
class _MidDescriptor:
39+
@overload
40+
def __get__(self, instance: Interval[_OrderableScalarT], owner: Any) -> float: ...
41+
@overload
42+
def __get__(
43+
self, instance: Interval[_OrderableTimesT], owner: Any
44+
) -> _OrderableTimesT: ...
45+
@overload
46+
def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ...
47+
48+
class IntervalMixin:
49+
@property
50+
def closed_left(self) -> bool: ...
51+
@property
52+
def closed_right(self) -> bool: ...
53+
@property
54+
def open_left(self) -> bool: ...
55+
@property
56+
def open_right(self) -> bool: ...
57+
mid: _MidDescriptor
58+
length: _LengthDescriptor
59+
@property
60+
def is_empty(self) -> bool: ...
61+
def _check_closed_matches(self, other: IntervalMixin, name: str = ...) -> None: ...
62+
63+
class Interval(IntervalMixin, Generic[_OrderableT]):
64+
@property
65+
def left(self: Interval[_OrderableT]) -> _OrderableT: ...
66+
@property
67+
def right(self: Interval[_OrderableT]) -> _OrderableT: ...
68+
@property
69+
def closed(self) -> IntervalClosedType: ...
70+
def __init__(
71+
self,
72+
left: _OrderableT,
73+
right: _OrderableT,
74+
closed: IntervalClosedType = ...,
75+
): ...
76+
def __hash__(self) -> int: ...
77+
@overload
78+
def __contains__(self: Interval[_OrderableTimesT], _OrderableTimesT) -> bool: ...
79+
@overload
80+
def __contains__(
81+
self: Interval[_OrderableScalarT], key: Union[int, float]
82+
) -> bool: ...
83+
def __repr__(self) -> str: ...
84+
def __str__(self) -> str: ...
85+
@overload
86+
def __add__(
87+
self: Interval[_OrderableTimesT], y: Timedelta
88+
) -> Interval[_OrderableTimesT]: ...
89+
@overload
90+
def __add__(self: Interval[int], y: int) -> Interval[int]: ...
91+
@overload
92+
def __add__(self: Interval[int], y: float) -> Interval[float]: ...
93+
@overload
94+
def __add__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
95+
@overload
96+
def __radd__(
97+
self: Interval[_OrderableTimesT], y: Timedelta
98+
) -> Interval[_OrderableTimesT]: ...
99+
@overload
100+
def __radd__(self: Interval[int], y: int) -> Interval[int]: ...
101+
@overload
102+
def __radd__(self: Interval[int], y: float) -> Interval[float]: ...
103+
@overload
104+
def __radd__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
105+
@overload
106+
def __sub__(
107+
self: Interval[_OrderableTimesT], y: Timedelta
108+
) -> Interval[_OrderableTimesT]: ...
109+
@overload
110+
def __sub__(self: Interval[int], y: int) -> Interval[int]: ...
111+
@overload
112+
def __sub__(self: Interval[int], y: float) -> Interval[float]: ...
113+
@overload
114+
def __sub__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
115+
@overload
116+
def __rsub__(
117+
self: Interval[_OrderableTimesT], y: Timedelta
118+
) -> Interval[_OrderableTimesT]: ...
119+
@overload
120+
def __rsub__(self: Interval[int], y: int) -> Interval[int]: ...
121+
@overload
122+
def __rsub__(self: Interval[int], y: float) -> Interval[float]: ...
123+
@overload
124+
def __rsub__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
125+
@overload
126+
def __mul__(self: Interval[int], y: int) -> Interval[int]: ...
127+
@overload
128+
def __mul__(self: Interval[int], y: float) -> Interval[float]: ...
129+
@overload
130+
def __mul__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
131+
@overload
132+
def __rmul__(self: Interval[int], y: int) -> Interval[int]: ...
133+
@overload
134+
def __rmul__(self: Interval[int], y: float) -> Interval[float]: ...
135+
@overload
136+
def __rmul__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
137+
@overload
138+
def __truediv__(self: Interval[int], y: int) -> Interval[int]: ...
139+
@overload
140+
def __truediv__(self: Interval[int], y: float) -> Interval[float]: ...
141+
@overload
142+
def __truediv__(self: Interval[float], y: Union[int, float]) -> Interval[float]: ...
143+
@overload
144+
def __floordiv__(self: Interval[int], y: int) -> Interval[int]: ...
145+
@overload
146+
def __floordiv__(self: Interval[int], y: float) -> Interval[float]: ...
147+
@overload
148+
def __floordiv__(
149+
self: Interval[float], y: Union[int, float]
150+
) -> Interval[float]: ...
151+
def overlaps(self: Interval[_OrderableT], other: Interval[_OrderableT]) -> bool: ...
152+
153+
def intervals_to_interval_bounds(
154+
intervals: np.ndarray, validate_closed: bool = ...
155+
) -> tuple[np.ndarray, np.ndarray, str]: ...
156+
157+
class IntervalTree(IntervalMixin):
158+
def __init__(
159+
self,
160+
left: np.ndarray,
161+
right: np.ndarray,
162+
closed: IntervalClosedType = ...,
163+
leaf_size: int = ...,
164+
): ...
165+
def get_indexer(self, target) -> npt.NDArray[np.intp]: ...
166+
def get_indexer_non_unique(
167+
self, target
168+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
169+
_na_count: int
170+
@property
171+
def is_overlapping(self) -> bool: ...
172+
@property
173+
def is_monotonic_increasing(self) -> bool: ...
174+
def clear_mapping(self) -> None: ...

pandas/core/indexes/interval.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,10 @@ def from_tuples(
321321
return cls._simple_new(arr, name=name)
322322

323323
# --------------------------------------------------------------------
324-
324+
# error: Return type "IntervalTree" of "_engine" incompatible with return type
325+
# "Union[IndexEngine, ExtensionEngine]" in supertype "Index"
325326
@cache_readonly
326-
def _engine(self) -> IntervalTree:
327+
def _engine(self) -> IntervalTree: # type: ignore[override]
327328
left = self._maybe_convert_i8(self.left)
328329
right = self._maybe_convert_i8(self.right)
329330
return IntervalTree(left, right, closed=self.closed)
@@ -515,7 +516,10 @@ def _maybe_convert_i8(self, key):
515516
left = self._maybe_convert_i8(key.left)
516517
right = self._maybe_convert_i8(key.right)
517518
constructor = Interval if scalar else IntervalIndex.from_arrays
518-
return constructor(left, right, closed=self.closed)
519+
# error: "object" not callable
520+
return constructor(
521+
left, right, closed=self.closed
522+
) # type: ignore[operator]
519523

520524
if scalar:
521525
# Timestamp/Timedelta

pandas/io/formats/style.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -3810,14 +3810,10 @@ def _highlight_between(
38103810
Return an array of css props based on condition of data values within given range.
38113811
"""
38123812
if np.iterable(left) and not isinstance(left, str):
3813-
left = _validate_apply_axis_arg(
3814-
left, "left", None, data # type: ignore[arg-type]
3815-
)
3813+
left = _validate_apply_axis_arg(left, "left", None, data)
38163814

38173815
if np.iterable(right) and not isinstance(right, str):
3818-
right = _validate_apply_axis_arg(
3819-
right, "right", None, data # type: ignore[arg-type]
3820-
)
3816+
right = _validate_apply_axis_arg(right, "right", None, data)
38213817

38223818
# get ops with correct boundary attribution
38233819
if inclusive == "both":

0 commit comments

Comments
 (0)