Skip to content

ENH: Imprpve interval_range and IntervalIndex #351

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 4 commits into from
Oct 3, 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
4 changes: 1 addition & 3 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ PandasScalar: TypeAlias = Union[
]
# Scalar: TypeAlias = Union[PythonScalar, PandasScalar]

DatetimeLike: TypeAlias = Union[
datetime.date, datetime.datetime, np.datetime64, Timestamp
]
DatetimeLike: TypeAlias = Union[datetime.datetime, np.datetime64, Timestamp]

# dtypes
NpDtype: TypeAlias = Union[
Expand Down
8 changes: 7 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ from pandas._typing import (
Dtype,
DtypeArg,
DtypeObj,
FillnaOptions,
HashableT,
IndexT,
Label,
Expand Down Expand Up @@ -155,7 +156,12 @@ class Index(IndexOpsMixin, PandasObject):
def symmetric_difference(
self, other: list[T1] | Index, result_name=..., sort=...
) -> Index: ...
def get_loc(self, key, tolerance=...): ...
def get_loc(
self,
key: Label,
method: FillnaOptions | Literal["nearest"] | None = ...,
tolerance=...,
): ...
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
def reindex(self, target, method=..., level=..., limit=..., tolerance=...): ...
def join(
Expand Down
94 changes: 77 additions & 17 deletions pandas-stubs/core/indexes/interval.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
from typing import Hashable
import datetime as dt
from typing import (
Any,
Hashable,
Literal,
Sequence,
Union,
overload,
)

import numpy as np
import pandas as pd
from pandas import Index
from pandas.core.indexes.extension import ExtensionIndex
from typing_extensions import TypeAlias

from pandas._libs.interval import (
Interval as Interval,
IntervalMixin as IntervalMixin,
)
from pandas._libs.tslibs.offsets import DateOffset
from pandas._typing import (
DatetimeLike,
DtypeArg,
FillnaOptions,
IntervalClosedType,
Label,
npt,
)

from pandas.core.dtypes.dtypes import IntervalDtype as IntervalDtype
from pandas.core.dtypes.generic import ABCSeries

_Edges: TypeAlias = Union[
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea

Sequence[int],
Sequence[float],
Sequence[DatetimeLike],
npt.NDArray[np.int_],
npt.NDArray[np.float_],
npt.NDArray[np.datetime64],
pd.Series[int],
pd.Series[float],
pd.Series[pd.Timestamp],
pd.Int64Index,
pd.DatetimeIndex,
]

class IntervalIndex(IntervalMixin, ExtensionIndex):
def __new__(
cls,
Expand All @@ -28,7 +58,7 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
@classmethod
def from_breaks(
cls,
breaks,
breaks: _Edges,
closed: IntervalClosedType = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -37,8 +67,8 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
@classmethod
def from_arrays(
cls,
left,
right,
left: _Edges,
right: _Edges,
closed: IntervalClosedType = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -47,37 +77,67 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
@classmethod
def from_tuples(
cls,
data,
data: Sequence[tuple[int, int]]
| Sequence[tuple[float, float]]
| Sequence[tuple[DatetimeLike, DatetimeLike]]
| npt.NDArray,
closed: IntervalClosedType = ...,
name: Hashable = ...,
copy: bool = ...,
dtype: IntervalDtype | None = ...,
) -> IntervalIndex: ...
def __contains__(self, key: Any) -> bool: ...
def astype(self, dtype: DtypeArg, copy: bool = ...) -> IntervalIndex: ...
@property
def inferred_type(self) -> str: ...
def memory_usage(self, deep: bool = ...) -> int: ...
@property
def is_overlapping(self) -> bool: ...
def get_loc(self, key, tolerance=...) -> int | slice | np.ndarray: ...
# Note: tolerance no effect. It is included in all get_loc so
# that signatures are consistent with base even though it is usually not used
def get_loc(
self,
key: Label,
method: FillnaOptions | Literal["nearest"] | None = ...,
tolerance=...,
) -> int | slice | npt.NDArray[np.bool_]: ...
def get_indexer(
self,
targetArrayLike,
method: str | None = ...,
target: Index,
method: FillnaOptions | Literal["nearest"] | None = ...,
limit: int | None = ...,
tolerance=...,
) -> np.ndarray: ...
) -> npt.NDArray[np.intp]: ...
def get_indexer_non_unique(
self, targetArrayLike
) -> tuple[np.ndarray, np.ndarray]: ...
self, target: Index
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
@property
def left(self) -> Index: ...
@property
def right(self) -> Index: ...
@property
def mid(self) -> Index: ...
@property
def length(self) -> Index: ...
def get_value(self, series: ABCSeries, key): ...
@property
def is_all_dates(self) -> bool: ...
def __lt__(self, other): ...
def __le__(self, other): ...
def __gt__(self, other): ...
def __ge__(self, other): ...

@overload
def interval_range(
start: int | float | None = ...,
end: int | float | None = ...,
periods: int | None = ...,
freq: int | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex: ...
@overload
def interval_range(
start=..., end=..., periods=..., freq=..., name=..., closed: str = ...
): ...
start: DatetimeLike | None = ...,
end: DatetimeLike | None = ...,
periods: int | None = ...,
freq: str | DateOffset | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex: ...
Loading