-
-
Notifications
You must be signed in to change notification settings - Fork 144
ENH: Improve typing for Interval #391
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
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
977f98f
ENH: Improve typing for Interval
bashtage dbb1735
ENH: Further refinements of interval
bashtage f1805f5
Attempt to make IntervalIndex generic
bashtage 4712cf5
MAINT: Merge main
bashtage 66846d3
ENH: Add more generic support
bashtage 6afb424
Merge main
bashtage c9ee580
ENH: Improve interval
bashtage cc45bf1
ENH: Improve from tuples
bashtage 794a577
MAINT: Merge main
bashtage af44ad4
Merge remote-tracking branch 'upstream/main' into scalars-interval
bashtage 657c1d2
CLN: Final clean ups
bashtage 684ef2e
CLN: Final clean ups
bashtage e2cc729
Merge remote-tracking branch 'upstream/main' into scalars-interval
bashtage f01a388
CLN: Final fixes
bashtage 6d15bba
Merge remote-tracking branch 'upstream/main' into scalars-interval
bashtage ee0003b
CLN: Final fixes
bashtage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[flake8] | ||
per-file-ignores = | ||
tests/test_scalars.py: W503 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import datetime as dt | ||
from typing import ( | ||
Any, | ||
Generic, | ||
Hashable, | ||
Literal, | ||
Sequence, | ||
TypeVar, | ||
Union, | ||
overload, | ||
) | ||
|
@@ -12,11 +14,15 @@ import numpy as np | |
import pandas as pd | ||
from pandas import Index | ||
from pandas.core.indexes.extension import ExtensionIndex | ||
from pandas.core.series import ( | ||
TimedeltaSeries, | ||
TimestampSeries, | ||
) | ||
from typing_extensions import TypeAlias | ||
|
||
from pandas._libs.interval import ( | ||
Interval as Interval, | ||
IntervalMixin as IntervalMixin, | ||
IntervalMixin, | ||
) | ||
from pandas._libs.tslibs.offsets import DateOffset | ||
from pandas._typing import ( | ||
|
@@ -25,55 +31,144 @@ from pandas._typing import ( | |
FillnaOptions, | ||
IntervalClosedType, | ||
Label, | ||
TimedeltaConvertibleTypes, | ||
np_ndarray_bool, | ||
npt, | ||
) | ||
|
||
from pandas.core.dtypes.dtypes import IntervalDtype as IntervalDtype | ||
from pandas.core.dtypes.generic import ABCSeries | ||
|
||
_Edges: TypeAlias = Union[ | ||
_EdgesInt: TypeAlias = Union[ | ||
Sequence[int], | ||
Sequence[float], | ||
Sequence[DatetimeLike], | ||
npt.NDArray[np.int_], | ||
npt.NDArray[np.float_], | ||
npt.NDArray[np.datetime64], | ||
npt.NDArray[np.int64], | ||
npt.NDArray[np.int32], | ||
npt.NDArray[np.intp], | ||
pd.Series[int], | ||
pd.Series[float], | ||
pd.Series[pd.Timestamp], | ||
pd.Int64Index, | ||
pd.DatetimeIndex, | ||
] | ||
_EdgesFloat: TypeAlias = Union[ | ||
Sequence[float] | npt.NDArray[np.float64] | pd.Series[float] | pd.Float64Index, | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the use of |
||
_EdgesTimestamp: TypeAlias = Union[ | ||
Sequence[DatetimeLike] | ||
| npt.NDArray[np.datetime64] | ||
| pd.Series[pd.Timestamp] | ||
| TimestampSeries | ||
| pd.DatetimeIndex | ||
] | ||
_EdgesTimedelta: TypeAlias = Union[ | ||
Sequence[pd.Timedelta] | ||
| npt.NDArray[np.timedelta64] | ||
| pd.Series[pd.Timedelta] | ||
| TimedeltaSeries | ||
| pd.TimedeltaIndex | ||
] | ||
|
||
_IntervalT = TypeVar( | ||
"_IntervalT", | ||
Interval[int], | ||
Interval[float], | ||
Interval[pd.Timestamp], | ||
Interval[pd.Timedelta], | ||
) | ||
|
||
class IntervalIndex(IntervalMixin, ExtensionIndex): | ||
class IntervalIndex(IntervalMixin, ExtensionIndex, Generic[_IntervalT]): | ||
def __new__( | ||
cls, | ||
data, | ||
data: Sequence[_IntervalT], | ||
closed: IntervalClosedType = ..., | ||
dtype: IntervalDtype | None = ..., | ||
copy: bool = ..., | ||
name: Hashable = ..., | ||
verify_integrity: bool = ..., | ||
): ... | ||
) -> IntervalIndex[_IntervalT]: ... | ||
# ignore[misc] here due to overlap, e.g., Sequence[int] and Sequence[float] | ||
@overload | ||
@classmethod | ||
def from_breaks( # type:ignore[misc] | ||
cls, | ||
breaks: _EdgesInt, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[int]]: ... | ||
@overload | ||
@classmethod | ||
def from_breaks( | ||
cls, | ||
breaks: _Edges, | ||
breaks: _EdgesFloat, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex: ... | ||
) -> IntervalIndex[Interval[float]]: ... | ||
@overload | ||
@classmethod | ||
def from_breaks( | ||
cls, | ||
breaks: _EdgesTimestamp, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[pd.Timestamp]]: ... | ||
@overload | ||
@classmethod | ||
def from_breaks( | ||
cls, | ||
breaks: _EdgesTimedelta, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[pd.Timedelta]]: ... | ||
# ignore[misc] here due to overlap, e.g., Sequence[int] and Sequence[float] | ||
@overload | ||
@classmethod | ||
def from_arrays( # type:ignore[misc] | ||
cls, | ||
left: _EdgesInt, | ||
right: _EdgesInt, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[int]]: ... | ||
@overload | ||
@classmethod | ||
def from_arrays( | ||
cls, | ||
left: _Edges, | ||
right: _Edges, | ||
left: _EdgesFloat, | ||
right: _EdgesFloat, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex: ... | ||
) -> IntervalIndex[Interval[float]]: ... | ||
@overload | ||
@classmethod | ||
def from_arrays( | ||
cls, | ||
left: _EdgesTimestamp, | ||
right: _EdgesTimestamp, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[pd.Timestamp]]: ... | ||
@overload | ||
@classmethod | ||
def from_arrays( | ||
cls, | ||
left: _EdgesTimedelta, | ||
right: _EdgesTimedelta, | ||
closed: IntervalClosedType = ..., | ||
name: Hashable = ..., | ||
copy: bool = ..., | ||
dtype: IntervalDtype | None = ..., | ||
) -> IntervalIndex[Interval[pd.Timedelta]]: ... | ||
@classmethod | ||
def from_tuples( | ||
cls, | ||
|
@@ -122,6 +217,30 @@ class IntervalIndex(IntervalMixin, ExtensionIndex): | |
def get_value(self, series: ABCSeries, key): ... | ||
@property | ||
def is_all_dates(self) -> bool: ... | ||
@overload # type: ignore[override] | ||
def __gt__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __gt__(self, other: object) -> bool: ... | ||
bashtage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@overload # type: ignore[override] | ||
def __ge__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __ge__(self, other: object) -> bool: ... | ||
@overload # type: ignore[override] | ||
def __le__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __le__(self, other: object) -> bool: ... | ||
@overload # type: ignore[override] | ||
def __lt__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __lt__(self, other: object) -> bool: ... | ||
@overload # type: ignore[override] | ||
def __eq__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __eq__(self, other: object) -> bool: ... | ||
bashtage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@overload # type: ignore[override] | ||
def __ne__(self, other: Interval | IntervalIndex) -> np_ndarray_bool: ... # type: ignore[misc] | ||
@overload | ||
def __ne__(self, other: object) -> bool: ... | ||
|
||
@overload | ||
def interval_range( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.