Skip to content

Commit a8bc6c6

Browse files
amotzopAmotz
and
Amotz
authored
Fixed typing on IntervalIndex functions (#174)
* Fixed typing on IntervalIndex functions * Used IntervalClosedType and fixes tests style issues Co-authored-by: Amotz <amotz@nonaya.bis>
1 parent e5e33bd commit a8bc6c6

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

pandas-stubs/core/indexes/interval.pyi

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
from typing import Hashable
2+
13
import numpy as np
2-
from pandas.core.indexes.base import Index
34
from pandas.core.indexes.extension import ExtensionIndex
45

56
from pandas._libs.interval import (
67
Interval as Interval,
78
IntervalMixin as IntervalMixin,
89
)
9-
from pandas._typing import DtypeArg
10+
from pandas._typing import (
11+
DtypeArg,
12+
IntervalClosedType,
13+
)
1014

15+
from pandas.core.dtypes.dtypes import IntervalDtype as IntervalDtype
1116
from pandas.core.dtypes.generic import ABCSeries as ABCSeries
1217

1318
class SetopCheck:
@@ -19,38 +24,46 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
1924
def __new__(
2025
cls,
2126
data,
22-
closed=...,
23-
dtype=...,
27+
closed: IntervalClosedType = ...,
28+
dtype: IntervalDtype | None = ...,
2429
copy: bool = ...,
25-
name=...,
30+
name: Hashable = ...,
2631
verify_integrity: bool = ...,
2732
): ...
2833
@classmethod
2934
def from_breaks(
30-
cls, breaks, closed: str = ..., name=..., copy: bool = ..., dtype=...
31-
): ...
35+
cls,
36+
breaks,
37+
closed: IntervalClosedType = ...,
38+
name: Hashable = ...,
39+
copy: bool = ...,
40+
dtype: IntervalDtype | None = ...,
41+
) -> IntervalIndex: ...
3242
@classmethod
3343
def from_arrays(
34-
cls, left, right, closed: str = ..., name=..., copy: bool = ..., dtype=...
35-
): ...
44+
cls,
45+
left,
46+
right,
47+
closed: IntervalClosedType = ...,
48+
name: Hashable = ...,
49+
copy: bool = ...,
50+
dtype: IntervalDtype | None = ...,
51+
) -> IntervalIndex: ...
3652
@classmethod
3753
def from_tuples(
38-
cls, data, closed: str = ..., name=..., copy: bool = ..., dtype=...
39-
): ...
40-
def __contains__(self, key) -> bool: ...
41-
def values(self): ...
42-
def __array_wrap__(self, result, context=...): ...
43-
def __reduce__(self): ...
44-
def astype(self, dtype: DtypeArg, copy: bool = ...) -> Index: ...
54+
cls,
55+
data,
56+
closed: IntervalClosedType = ...,
57+
name: Hashable = ...,
58+
copy: bool = ...,
59+
dtype: IntervalDtype | None = ...,
60+
) -> IntervalIndex: ...
61+
def astype(self, dtype: DtypeArg, copy: bool = ...) -> IntervalIndex: ...
4562
@property
4663
def inferred_type(self) -> str: ...
4764
def memory_usage(self, deep: bool = ...) -> int: ...
48-
def is_monotonic(self) -> bool: ...
49-
def is_monotonic_increasing(self) -> bool: ...
50-
def is_monotonic_decreasing(self) -> bool: ...
51-
def is_unique(self): ...
5265
@property
53-
def is_overlapping(self): ...
66+
def is_overlapping(self) -> bool: ...
5467
def get_loc(
5568
self, key, method: str | None = ..., tolerance=...
5669
) -> int | slice | np.ndarray: ...
@@ -65,15 +78,6 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
6578
self, targetArrayLike
6679
) -> tuple[np.ndarray, np.ndarray]: ...
6780
def get_value(self, series: ABCSeries, key): ...
68-
def where(self, cond, other=...): ...
69-
def delete(self, loc): ...
70-
def insert(self, loc, item): ...
71-
def take(
72-
self, indices, axis: int = ..., allow_fill: bool = ..., fill_value=..., **kwargs
73-
): ...
74-
def __getitem__(self, value): ...
75-
def argsort(self, *args, **kwargs): ...
76-
def equals(self, other) -> bool: ...
7781
@property
7882
def is_all_dates(self) -> bool: ...
7983
def __lt__(self, other): ...

tests/test_interval_index.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import annotations
2+
3+
import pandas as pd
4+
from typing_extensions import assert_type
5+
6+
from tests import check
7+
8+
9+
def test_from_breaks() -> None:
10+
ind1: pd.IntervalIndex = pd.IntervalIndex.from_breaks([0, 1, 2, 3], name="test")
11+
ind2: pd.IntervalIndex = pd.IntervalIndex.from_breaks(
12+
[0, 1, 2, 3], closed="right", name=123
13+
)
14+
15+
16+
def test_from_arrays() -> None:
17+
ind1: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
18+
[0, 1, 2], [1, 2, 3], name="test"
19+
)
20+
ind2: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
21+
[0, 1, 2], [1, 2, 3], closed="right", name=123
22+
)
23+
24+
25+
def test_from_tuples() -> None:
26+
ind1: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
27+
[(0, 1), (1, 2), (2, 3)], name="test"
28+
)
29+
ind2: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
30+
[(0, 1), (1, 2), (2, 3)], closed="right", name=123
31+
)
32+
33+
34+
def test_is_overlapping() -> None:
35+
ind = pd.IntervalIndex.from_tuples([(0, 2), (1, 3), (4, 5)])
36+
check(assert_type(ind.is_overlapping, bool), bool)

0 commit comments

Comments
 (0)