Skip to content

Make IndexOpsMixin (and Index) generic #760

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 25 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4929ecb
interpolate method
twoertwein Aug 2, 2023
2775a58
WIP
twoertwein Aug 2, 2023
9e40ddf
mypy handles Never differently - at least asssert that the method nev…
twoertwein Aug 3, 2023
68abe4d
use Self
twoertwein Aug 3, 2023
2cfbdb0
a few more S1/Self
twoertwein Aug 3, 2023
ebcc389
fix tests
twoertwein Aug 3, 2023
24d42fa
more tests
twoertwein Aug 3, 2023
f3802d3
add an ignore to let mypy pass
twoertwein Aug 3, 2023
92eab41
compat with old python versions
twoertwein Aug 3, 2023
6e04d8e
fix CategoricalIndex; MultiIndex should probably be Index[tuple[S1, .…
twoertwein Aug 3, 2023
e7a2d3b
Revert "interpolate method"
twoertwein Aug 3, 2023
d12a72a
many more overloads for subclasses (I wish pandas would not handle su…
twoertwein Aug 4, 2023
87c62d0
remove PandasObject - it caused the pyright issues but it provides no…
twoertwein Aug 4, 2023
9ce5fc4
works (except for np.ndarray)
twoertwein Aug 4, 2023
05b9c15
Merge remote-tracking branch 'upstream/main' into iter_interpolate
twoertwein Aug 8, 2023
d32f534
address the easy-to-fix comments
twoertwein Aug 8, 2023
4f13109
overloads for numpy
twoertwein Aug 9, 2023
bd39625
did numexpr break the CI?
twoertwein Aug 9, 2023
8c48567
lie: df.columns -> Index[str]
twoertwein Aug 9, 2023
67f5e8d
new ruff has far more pyi rules
twoertwein Aug 9, 2023
566c918
Make it clear that both mypy&pyright infer Never; but only pyright de…
twoertwein Aug 10, 2023
ff5ee58
use type aliases instead of npt
twoertwein Aug 10, 2023
c8d2019
fix issue with floordiv for mypy
Dr-Irv Aug 13, 2023
585c93b
fix comment in arraylike
Dr-Irv Aug 13, 2023
7ac1ea5
Merge pull request #3 from Dr-Irv/pr760
twoertwein Aug 14, 2023
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.278
rev: v0.0.282
hooks:
- id: ruff
args: [
Expand Down
17 changes: 6 additions & 11 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import numpy as np
import pandas as pd
from pandas import (
DatetimeIndex,
Index,
PeriodIndex,
Series,
TimedeltaIndex,
)
from pandas.core.indexes.base import (
_FloatIndexType,
_IntIndexType,
)
from pandas.core.series import (
TimedeltaSeries,
TimestampSeries,
Expand Down Expand Up @@ -245,7 +242,7 @@ class Timedelta(timedelta):
@overload
def __mul__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __mul__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __mul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __rmul__(self, other: float) -> Timedelta: ...
@overload
Expand All @@ -255,7 +252,7 @@ class Timedelta(timedelta):
@overload
def __rmul__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __rmul__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __rmul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
# Override due to more types supported than dt.timedelta
# error: Signature of "__floordiv__" incompatible with supertype "timedelta"
@overload # type: ignore[override]
Expand All @@ -271,9 +268,7 @@ class Timedelta(timedelta):
self, other: npt.NDArray[np.timedelta64]
) -> npt.NDArray[np.int_]: ...
@overload
def __floordiv__(
self, other: _IntIndexType | _FloatIndexType
) -> TimedeltaIndex: ...
def __floordiv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __floordiv__(self, other: Series[int]) -> TimedeltaSeries: ...
@overload
Expand Down Expand Up @@ -306,7 +301,7 @@ class Timedelta(timedelta):
@overload
def __truediv__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __truediv__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __truediv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
def __rtruediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
# Override due to more types supported than dt.timedelta
@overload
Expand Down Expand Up @@ -338,7 +333,7 @@ class Timedelta(timedelta):
@overload
def __mod__(self, other: Series[int] | Series[float]) -> TimedeltaSeries: ...
@overload
def __mod__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __mod__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __mod__(
self, other: npt.NDArray[np.integer] | npt.NDArray[np.floating]
Expand Down
20 changes: 20 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,26 @@ StataDateFormat: TypeAlias = Literal[
]

FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"]
InterpolateOptions: TypeAlias = Literal[
"linear",
"time",
"index",
"pad",
"nearest",
"zero",
"slinear",
"quadratic",
"cubic",
"barycentric",
"polynomial",
"krogh",
"piecewise_polynomial",
"spline",
"pchip",
"akima",
"cubicspline",
"from_derivatives",
]
ReplaceMethod: TypeAlias = Literal["pad", "ffill", "bfill"]
SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition: TypeAlias = Literal["first", "last"]
Expand Down
21 changes: 11 additions & 10 deletions pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import (
Generic,
Iterator,
Literal,
)

Expand All @@ -9,8 +10,10 @@ from pandas.core.accessor import DirNamesMixin
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays import ExtensionArray
from pandas.core.arrays.categorical import Categorical
from typing_extensions import Self

from pandas._typing import (
S1,
AxisIndex,
NaPosition,
NDFrameT,
Expand All @@ -28,16 +31,16 @@ class SelectionMixin(Generic[NDFrameT]):
def ndim(self) -> int: ...
def __getitem__(self, key): ...

class IndexOpsMixin(OpsMixin):
class IndexOpsMixin(OpsMixin, Generic[S1]):
__array_priority__: int = ...
def transpose(self, *args, **kwargs) -> IndexOpsMixin: ...
def transpose(self, *args, **kwargs) -> Self: ...
@property
def T(self) -> IndexOpsMixin: ...
def T(self) -> Self: ...
@property
def shape(self) -> tuple: ...
@property
def ndim(self) -> int: ...
def item(self): ...
def item(self) -> S1: ...
@property
def nbytes(self) -> int: ...
@property
Expand All @@ -61,9 +64,9 @@ class IndexOpsMixin(OpsMixin):
def argmin(
self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs
) -> np.int64: ...
def tolist(self) -> list: ...
def to_list(self) -> list: ...
def __iter__(self): ...
def tolist(self) -> list[S1]: ...
def to_list(self) -> list[S1]: ...
def __iter__(self) -> Iterator[S1]: ...
@property
def hasnans(self) -> bool: ...
def value_counts(
Expand All @@ -87,6 +90,4 @@ class IndexOpsMixin(OpsMixin):
def searchsorted(
self, value, side: Literal["left", "right"] = ..., sorter=...
) -> int | list[int]: ...
def drop_duplicates(
self, *, keep: NaPosition | Literal[False] = ...
) -> IndexOpsMixin: ...
def drop_duplicates(self, *, keep: NaPosition | Literal[False] = ...) -> Self: ...
7 changes: 4 additions & 3 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ from pandas._typing import (
IndexingInt,
IndexLabel,
IndexType,
InterpolateOptions,
IntervalClosedType,
IntervalT,
JoinHow,
Expand Down Expand Up @@ -1704,7 +1705,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def interpolate(
self,
method: _str = ...,
method: InterpolateOptions = ...,
*,
axis: Axis = ...,
limit: int | None = ...,
Expand All @@ -1717,7 +1718,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def interpolate(
self,
method: _str = ...,
method: InterpolateOptions = ...,
*,
axis: Axis = ...,
limit: int | None = ...,
Expand All @@ -1730,7 +1731,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def interpolate(
self,
method: _str = ...,
method: InterpolateOptions = ...,
*,
axis: Axis = ...,
limit: int | None = ...,
Expand Down
11 changes: 5 additions & 6 deletions pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ from pandas.core.base import (
PandasObject,
)
from pandas.core.frame import DataFrame
from pandas.core.indexes.base import _IntIndexType
from pandas.core.series import (
PeriodSeries,
Series,
Expand All @@ -43,7 +42,7 @@ from pandas._typing import (
class Properties(PandasDelegate, PandasObject, NoNewAttributesMixin):
def __init__(self, data: Series, orig) -> None: ...

_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], _IntIndexType)
_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], Index[int])

class _DayLikeFieldOps(Generic[_DTFieldOpsReturnType]):
@property
Expand Down Expand Up @@ -302,7 +301,7 @@ class TimedeltaProperties(
): ...

_PeriodDTReturnTypes = TypeVar("_PeriodDTReturnTypes", TimestampSeries, DatetimeIndex)
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", Series[int], _IntIndexType)
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", Series[int], Index[int])
_PeriodStrReturnTypes = TypeVar("_PeriodStrReturnTypes", Series[str], Index)
_PeriodDTAReturnTypes = TypeVar("_PeriodDTAReturnTypes", DatetimeArray, DatetimeIndex)
_PeriodPAReturnTypes = TypeVar("_PeriodPAReturnTypes", PeriodArray, PeriodIndex)
Expand Down Expand Up @@ -335,8 +334,8 @@ class _PeriodProperties(
) -> _PeriodPAReturnTypes: ...

class PeriodIndexFieldOps(
_DayLikeFieldOps[_IntIndexType],
_PeriodProperties[DatetimeIndex, _IntIndexType, Index, DatetimeIndex, PeriodIndex],
_DayLikeFieldOps[Index[int]],
_PeriodProperties[DatetimeIndex, Index[int], Index, DatetimeIndex, PeriodIndex],
): ...
class PeriodProperties(
Properties,
Expand Down Expand Up @@ -382,7 +381,7 @@ class TimestampProperties(
class DatetimeIndexProperties(
Properties,
_DatetimeNoTZProperties[
_IntIndexType,
Index[int],
np_ndarray_bool,
DatetimeIndex,
np.ndarray,
Expand Down
Loading