Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ dmypy.json
.pyre/
/poetry.lock
.idea/**/*

# duplication of stub specific objects upon testing
tests/_typing.pyi
88 changes: 46 additions & 42 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from os import PathLike
from re import Pattern
import sys
from typing import (
TYPE_CHECKING,
Any,
Generic,
Literal,
Expand All @@ -25,7 +26,6 @@ from typing import (
overload,
)

from _typeshed import _T_contra
import numpy as np
from numpy import typing as npt
import pandas as pd
Expand Down Expand Up @@ -87,13 +87,15 @@ HashableT5 = TypeVar("HashableT5", bound=Hashable)

ArrayLike: TypeAlias = ExtensionArray | npt.NDArray[Any]
AnyArrayLike: TypeAlias = ArrayLike | Index | Series
AnyArrayLikeInt: TypeAlias = (
IntegerArray | Index[int] | Series[int] | npt.NDArray[np.integer]
)
if TYPE_CHECKING: # noqa: PYI002
AnyArrayLikeInt: TypeAlias = (
IntegerArray | Index[int] | Series[int] | npt.NDArray[np.integer]
)

# list-like

_T_co = TypeVar("_T_co", covariant=True)
_T_contra = TypeVar("_T_contra", contravariant=True)

class SequenceNotStr(Protocol[_T_co]):
@overload
Expand Down Expand Up @@ -966,8 +968,9 @@ class SupportsDType(Protocol[GenericT_co]):
# Similar to npt.DTypeLike but leaves out np.dtype and None for use in overloads
DTypeLike: TypeAlias = type[Any] | tuple[Any, Any] | list[Any] | str

IndexType: TypeAlias = slice | np_ndarray_anyint | Index | list[int] | Series[int]
MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]
if TYPE_CHECKING: # noqa: PYI002
IndexType: TypeAlias = slice | np_ndarray_anyint | Index | list[int] | Series[int]
MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]

# Scratch types for generics

Expand Down Expand Up @@ -1039,48 +1042,49 @@ Function: TypeAlias = np.ufunc | Callable[..., Any]
# shared HashableT and HashableT#. This one can be used if the identical
# type is need in a function that uses GroupByObjectNonScalar
_HashableTa = TypeVar("_HashableTa", bound=Hashable)
ByT = TypeVar(
"ByT",
bound=str
| bytes
| datetime.date
| datetime.datetime
| datetime.timedelta
| np.datetime64
| np.timedelta64
| bool
| int
| float
| complex
| Scalar
| Period
| Interval[int | float | Timestamp | Timedelta]
| tuple,
)
# Use a distinct SeriesByT when using groupby with Series of known dtype.
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
SeriesByT = TypeVar(
"SeriesByT",
bound=str
| bytes
| datetime.date
| bool
| int
| float
| complex
| datetime.datetime
| datetime.timedelta
| Period
| Interval[int | float | Timestamp | Timedelta],
)
if TYPE_CHECKING: # noqa: PYI002
ByT = TypeVar(
"ByT",
bound=str
| bytes
| datetime.date
| datetime.datetime
| datetime.timedelta
| np.datetime64
| np.timedelta64
| bool
| int
| float
| complex
| Scalar
| Period
| Interval[int | float | Timestamp | Timedelta]
| tuple,
)
# Use a distinct SeriesByT when using groupby with Series of known dtype.
# Essentially, an intersection between Series S1 TypeVar, and ByT TypeVar
SeriesByT = TypeVar(
"SeriesByT",
bound=str
| bytes
| datetime.date
| bool
| int
| float
| complex
| datetime.datetime
| datetime.timedelta
| Period
| Interval[int | float | Timestamp | Timedelta],
)
GroupByObjectNonScalar: TypeAlias = (
tuple
| list[_HashableTa]
| Function
| list[Function]
| list[Series]
| np.ndarray
| list[np.ndarray]
| np_ndarray
| list[np_ndarray]
| Mapping[Label, Any]
| list[Mapping[Label, Any]]
| list[Index]
Expand Down
3 changes: 3 additions & 0 deletions pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from typing import (
Literal,
Protocol,
TypeAlias,
TypeVar,
final,
overload,
type_check_only,
Expand Down Expand Up @@ -53,6 +54,8 @@ from pandas._typing import (
)
from pandas.util._decorators import cache_readonly

T_INTERVAL_NP = TypeVar("T_INTERVAL_NP", bound=np.bytes_ | np.str_)

class NoNewAttributesMixin:
def __setattr__(self, key: str, value: Any) -> None: ...

Expand Down
11 changes: 10 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import numpy as np
from pandas.core.arrays.boolean import BooleanArray
from pandas.core.arrays.floating import FloatingArray
from pandas.core.base import (
T_INTERVAL_NP,
ArrayIndexTimedeltaNoSeq,
ElementOpsMixin,
IndexComplex,
Expand Down Expand Up @@ -1189,6 +1190,14 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):

@type_check_only
class _IndexSubclassBase(Index[S1], Generic[S1, GenericT_co]):
@overload
def to_numpy(
self: _IndexSubclassBase[Interval],
dtype: type[T_INTERVAL_NP],
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray: ...
@overload
def to_numpy(
self,
Expand All @@ -1206,7 +1215,7 @@ class _IndexSubclassBase(Index[S1], Generic[S1, GenericT_co]):
**kwargs: Any,
) -> np_1darray[GenericT]: ...
@overload
def to_numpy(
def to_numpy( # pyright: ignore[reportIncompatibleMethodOverride]
self,
dtype: DTypeLike,
copy: bool = False,
Expand Down
29 changes: 22 additions & 7 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ from typing import (
NoReturn,
Protocol,
TypeAlias,
TypeVar,
final,
overload,
type_check_only,
Expand Down Expand Up @@ -71,6 +70,7 @@ from pandas.core.arrays.datetimes import DatetimeArray
from pandas.core.arrays.floating import FloatingArray
from pandas.core.arrays.timedeltas import TimedeltaArray
from pandas.core.base import (
T_INTERVAL_NP,
ArrayIndexSeriesTimedeltaNoSeq,
ArrayIndexTimedeltaNoSeq,
ElementOpsMixin,
Expand Down Expand Up @@ -189,6 +189,7 @@ from pandas._typing import (
MaskType,
NaPosition,
NsmallestNlargestKeep,
NumpyStrDtypeArg,
ObjectDtypeArg,
PandasAstypeComplexDtypeArg,
PandasAstypeFloatDtypeArg,
Expand Down Expand Up @@ -251,8 +252,6 @@ from pandas.core.dtypes.dtypes import CategoricalDtype

from pandas.plotting import PlotAccessor

_T_INTERVAL_NP = TypeVar("_T_INTERVAL_NP", bound=np.bytes_ | np.str_)

@type_check_only
class _SupportsAdd(Protocol[_T_co]):
def __add__(self, value: Self, /) -> _T_co: ...
Expand Down Expand Up @@ -4503,7 +4502,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray_bytes: ...
) -> np_1darray: ...
@overload
def to_numpy(
self: Series[Interval],
Expand All @@ -4515,11 +4514,11 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def to_numpy(
self: Series[Interval],
dtype: type[_T_INTERVAL_NP],
dtype: type[T_INTERVAL_NP],
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray[_T_INTERVAL_NP]: ...
) -> np_1darray: ...
@overload
def to_numpy(
self: Series[int],
Expand Down Expand Up @@ -4555,12 +4554,28 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def to_numpy(
self: Series[_str],
dtype: DTypeLike | None = None,
dtype: NumpyStrDtypeArg,
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray_str: ...
@overload
def to_numpy(
self: Series[_str],
dtype: DTypeLike,
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray: ...
@overload
def to_numpy(
self: Series[_str],
dtype: None = None,
copy: bool = False,
na_value: Scalar = ...,
**kwargs: Any,
) -> np_1darray_object: ...
@overload
def to_numpy(
self: Series[bytes],
dtype: DTypeLike | None = None,
Expand Down
Loading